Got class/interface level and method mapping
@RequestMapping(value = "/post")
public interface PostApi {
//to get /posts
@RequestMapping(value = "s")
ResponseEntity getAll();
}
Basically I want to add character 's' on /post and get /posts, how this is possible
CodePudding user response:
You can't change the context path, instead remove "/post" then use below "/post" and "/posts" on different tasks.
CodePudding user response:
public interface PostApi {
//to get /posts
@RequestMapping(value = "/posts")
ResponseEntity getAll();
@GetMapping(value = "/post/{id}")
ResponseEntity getById(@PathParam Long id);
}
CodePudding user response:
you can not concatenate the path values into a single path string, since they are seen as objects in the URI specification and not multiple just strings. (if you like RFCs, check this: https://datatracker.ietf.org/doc/html/rfc3986#section-3.3 )
so basically your class-level Mapping already sets a hierarchy by design, and the method can only configure a lower level inside that hierarchy ((earlier) spring documentations featured this explanation: "Method-level mappings are only allowed to narrow the mapping expressed at the class level (if any)."
the only way to work around this is actually to remove the class mapping and add individual mappings to either method (as stated by others). as a side effect: this makes your code much better readable, so reviewers (or yourself in 3 months) do not have to concat the full path in your mind whily trying to understand the controller