Spring MVC Request mapping, can this be dynamic/configurable?

前端 未结 5 815
慢半拍i
慢半拍i 2021-02-19 06:37

With Spring MVC, I know how you set the RequestMapping in every controller and method/action.

But what if I wanted this to be configurable, so for example I

5条回答
  •  渐次进展
    2021-02-19 07:10

    Also another solution might be to create a custom annotation that holds the already defined path on the @RequestMapping and also the new one to apply, let's say @ApiRestController.

    Then, before the Spring context loads, the @Controller classes can be changed to have their annotation values changed at runtime by the new one (with the desired path). By doing this, Spring will load the enhanced request mapping and not the default one.

    Created a small project to exemplify this for someone that needs this in the future https://gitlab.com/jdiasamaro/spring-api-rest-controllers.

    Hope it helps. Cheers.

提交回复
热议问题