RequestMapping Override on SpringMVC

Looking through the source for our applications, I found a common Spring MVC controller that displays the key and configuration values ​​copied and pasted several times. The class definitions are exactly the same, except for the RequestMapping value, since each application wants to have this page under different URLs.

I want to move this controller to a shared library and provide the default RequestMapping value.

@Controller @RequestMapping (value="/property") public class CommonPropertyController { .... } 

How does each application override this value if they want to use their own URL pattern?

+6
source share
1 answer

Looking at the source code, I got an idea of ​​how to do this without returning to the manual description of the handler (pre-annotation) (which is also a way to implement what you need).

Spring allows you to use property placeholder configurators in @RequestMapping values. Thus, you can use this fact and define @RequestMapping as:

 @Controller @RequestMapping("${routing.property.path}") public class CommonPropertyController { .... } 

Then you can simply define the PropertySourcesPlaceholderConfigurer with the right properties in the context of your application, and you are good to go.


UPDATE You can also specify the default value using the property placeholder if you want to have a fallback if the property is not specified:

 @RequestMapping("${routing.property.path:/property}") 
+11
source

Source: https://habr.com/ru/post/947381/


All Articles