Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding RequestMapping on SpringMVC controller

Tags:

spring-mvc

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

I want to move this controller into a common library, and provide a default RequestMapping value.

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

How would each application override this value if they want to use their own url pattern?

like image 750
ltfishie Avatar asked Jun 15 '13 22:06

ltfishie


1 Answers

Looking at the source code I got an idea how to do it without having to go back to manual (pre-annotation) handler definition (which is also a way how to implement what you need).

Spring allows you to use property placeholder configurers in @RequestMapping values. So it is possible to use that fact and define @RequestMapping like:

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

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


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

@RequestMapping("${routing.property.path:/property}")
like image 92
Pavel Horal Avatar answered Nov 10 '22 22:11

Pavel Horal