Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

In brief, I would like to have the same feature as JAX-RS @ApplicationPath("/api") in Spring Web MVC, is this possible?

like image 650
Anthony O. Avatar asked Nov 08 '22 14:11

Anthony O.


1 Answers

One of the possible solution(work around) is to use Parent Class with mapping

@RequestMapping(path="/api")
abstract class BaseController{
    ....
}

Other controllers can extend it

class OtherController extends BaseController {
  @RequestMapping(path="/clients")
  public ....clients(....){
   .....
  }
}

Please note that @RequestMapping will get overridden if you place it on top of any child class. Like explained here and here

like image 200
shakeel Avatar answered Nov 14 '22 21:11

shakeel