I\'m trying to to mix mvc and rest in a single spring boot project.
I want to set base path for all rest controllers (eg. example.com/api) in a single place (I don\'
I couldn't believe how complicate the answer to this seemingly simple question is. Here are some references:
There are many differnt things to consider:
server.context-path=/api in application.properties you can configure a prefix for everything.(Its server.context-path not server.contextPath !)spring.data.rest.base-path in application.properties. But plain @RestController won't take this into account. According to the spring data rest documentation there is an annotation @BasePathAwareController that you can use for that. But I do have problems in connection with Spring-security when I try to secure such a controller. It is not found anymore.Another workaround is a simple trick. You cannot prefix a static String in an annotation, but you can use expressions like this:
@RestController
public class PingController {
/**
* Simple is alive test
* @return {"Hello":"World"}
*/
@RequestMapping("${spring.data.rest.base-path}/_ping")
public String isAlive() {
return "{\"Hello\":\"World\"}";
}
}