Reputation: 1225
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.
Upvotes: 20
Views: 20189
Reputation: 137
You can try using @path annotation to consider multiple URI as single URI.
@Path("sampleresource/{Filepath:.*}")
public interface SampleResource {
@GET
@Produces({ MediaType.TEXT_PLAIN })
@Path("/hello")
public Response sayHello();
}
Upvotes: 2
Reputation: 5503
See my answer to a similar question. How to configure a default @RestController URI prefix for all controllers? You can create a custom annotation and just perform mappings based on it.
Upvotes: 0
Reputation: 8057
You can try to create your custom annotation which would include @RestController
and @RequestMapping
:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@RestConntroller
@RequestMapping(value = "/api" )
@interface MyController {
}
Upvotes: 6
Reputation: 1549
Why don't you put @RequestMapping("/api") on all RestControllers?
@RestController
@RequestMapping("/api")
public class UserApi {
@RequestMapping("/user")
public String user() {
...
}
}
Upvotes: 14