Spring request a mapping to another method for a specific path variable value

@Controller
@RequestMapping("/authors")
public class AuthorController {
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Author getAuthor(
        final HttpServletRequest request,
        final HttpServletResponse response,
        @PathVariable final String id)
    {
        // Returns a single Author by id
        return null;
    }

    @RequestMapping(value = "/{id}/author-properties", method = RequestMethod.GET)
    public AuthorProperties getAuthorProperties(
        final HttpServletRequest request,
        final HttpServletResponse response,
        @PathVariable final String id)
    {
        // Returns a single Author List of properties
        return null;
    }

    @RequestMapping // How to map /authors/*/author-properties to this method ????
    public List<AuthorProperties> listAuthorProperties(
        final HttpServletRequest request,
        final HttpServletResponse response)
    {
        // Returns a single Author List of properties
        return null;
    }
}

class Author {
    String propertiesUri;
    // other fields
}

class AuthorProperties {
    String authorUri;
    // other fields
}

Basically I need:

  • / authors - listing all authors
  • / authors / 123 - select author by id 123
  • / authors / 123 / author-properties - select the AuthorProperties object for the author 123
  • / authors / * / author-properties - selection of the AuthorProperties list for all authors

When i tried

@RequestMapping(value = "/*/author-properties", method = RequestMethod.GET)

It still matched the /authors/*/author-propertiesgetAuthorProperties method with the path variable value as "*".

+4
source share
3 answers

See if it works

@RequestMapping(value = "/{id:.*}/author-properties", method = RequestMethod.GET)
0
source

, :

@RequestMapping("/{authorId:\\d+}/author-properties")
public String authorProperties(@PathVariable String authorId) {}

URL-, .

:

@RequestMapping("/*/author-properties")
public String allProperties() { }

Hovewer * , /foo/author-properties. , - :

@RequestMapping("/{all:\\*}/author-properties")
public String allProperties() { }
0

AuthorProperties , , URI : "/author-properties". , , - , .

0

Source: https://habr.com/ru/post/1609288/


All Articles