I also forced a switch from JAX-RS to Spring-MVC.
I'm still looking for an elegant way to do this the same as with JAX-RS.
I share what I tried.
@RestController @RequestMapping("parents") public class ParentsController { @RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE}) public ResponseEntity<List<Parent>> read() { } @RequestMapping(method = RequestMethod.GET, path = "/{id:\\d+}", produces = {MediaType.APPLICATION_JSON_VALUE}) public ResponseEntity<Parent> read(@PathVariable("id") final long id) { } }
And ChildrenController .
@RestController @RequestMapping("/parents/{parentId:\\d+}/children") public class ChildrenController { @RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE}) @ResponseBody public List<Child> read(@PathVariable("parentId") final long parentId) { } @RequestMapping(method = RequestMethod.GET, path = "/{id:\\d+}", produces = {MediaType.APPLICATION_JSON_VALUE}) @ResponseBody public Child read(@PathVariable("parentId") final long parentId, @PathVariable("id") final long id) { } }
Two problems that I found
@PathVariable not applicable to fields.
I just can't do @PathVariable("parentId") private long parentId;
No free will for multiple display for ChildrenController
The beauty of JAX-RS is that we can display the ChildrenController for different paths, for example, even ChildrenController has the @Path class with it.
@Path("/children"); public class ChildrenResource { } @Path("/parents") public class ParentsResource { @Path("/{id}/children") public ChildrenResource resourceChildren() { } } /children /parents/{id}/children
source share