I am using spring boot to write an api and I would like to map all my resources behind a common base path (/api in this case). However I don't want to annotate each RestController class to do this (by annotating it with @RequestMapping for example). I have thought about the following solutions but they all have a down side that i would rather not want:
So does anyone know how to do this without the disadvantages the solutions have that i summed. The project will only expose a REST-based backend and will not server any static content (don't know if this influences the possible solutions). The Restcontrollers are also divided over multiple packages.
Thanks.
You cannot. A URL can only be mapped to a single controller. It has to be unique.
Then, we only need to apply the annotation to each controller we want to prefix: @Controller @ApiPrefixController public class SomeController { @RequestMapping("/users") @ReponseBody public String getAll(){ // ... } }
The RestController allows to handle all REST APIs such as GET, POST, Delete, PUT requests. Spring Initializr is a web-based tool using which we can easily generate the structure of the Spring Boot project. It also provides various different features for the projects expressed in a metadata model.
Why don't you put @RequestMapping("/api") on all RestControllers?
@RestController
@RequestMapping("/api")
public class UserApi {
@RequestMapping("/user")
public String user() {
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With