ltfishie
ltfishie

Reputation: 2985

Overriding RequestMapping on SpringMVC controller

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?

Upvotes: 7

Views: 5512

Answers (1)

Pavel Horal
Pavel Horal

Reputation: 18224

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}")

Upvotes: 12

Related Questions