How to set a context root for all @RestController in Spring Web MVC?

Ant*_* O. 5 spring spring-mvc spring-boot spring-web

I'm looking to define /api as the root context for all my @RestControllers and don't want to specify it for each of them.

For instance, I would like to write @RestController("/clients") instead of @RestController("/api/clients") and make my request mappings available at /api/clients but have my static resources (in /main/resources/static) still served at /**.

So, setting server.servlet.contextPath=/api in application.properties is not a solution for my use case because the static resources will be served at /api, which I don't want.

简而言之,我想@ApplicationPath("/api")在 Spring Web MVC 中拥有与 JAX-RS 相同的功能,这可能吗?

sha*_*eel 1

可能的解决方案之一(解决方法)是将父类与映射一起使用

@RequestMapping(path="/api")
abstract class BaseController{
    ....
}
Run Code Online (Sandbox Code Playgroud)

其他控制器可以扩展它

class OtherController extends BaseController {
  @RequestMapping(path="/clients")
  public ....clients(....){
   .....
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,如果将 @RequestMapping 放置在任何子类之上,它将被覆盖。就像这里这里解释的那样