Announcement Announcement Module
No announcement yet.
Multiple instances of annotated Controller with RequestMapping Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple instances of annotated Controller with RequestMapping

    Using Spring 3.1 with MVC Java Config, I'm attempting to deploy an application with multiple instances of the same Controller, mapped to different endpoints. My Controller methods are also annotated with RequestMapping, but I can't figure out how to have the same Controller used twice.

    My controller code looks like this:
    public class SearchController {
        private SearchService searchService;
        public SearchController(earchService searchService) {
            this.searchService = searchService;
        public ResponseEntity<Response> handle(SearchCommand command) {
                // Search code
                return new ResponseEntity<Response>(...);
    So, what I'd like to do is be able to establish any number of different endpoints to search. The Controller code is the same, I just want to configure different instances of it wired with different instances of the SearchService depending on what type is being searched:
    • /places/search
    • /events/search
    • /restaurants/search

    Using the old xml config, I would have previously used the SimpleUrlHandlerMapping and configured several SearchController beans for each mapping. I know according to this ( that this is no longer possible with the Spring MVC 3.1 way, so what's the alternative?

  • #2
    And why wouldn't that be possible? Simple remove the /search from the RequestMapipng and use the SimpleUrlHandlerMapping to map the urls. There is nothing preventing you from doing so.


    • #3
      Well this would work at the controller level with my simple example that only has one method, but I actually have multiple methods in my controller, each mapped with a different RequestMapping (GET, POST, /edit, /delete, etc...). Is it possible to do this with mappings down to the method level? This seems to be more the Spring 3.1 way - having Controllers as pretty much pojos with urls mapped to methods.


      • #4
        I really suggest you read the reference guide...

        In your mappings you can use wildcards so you can add something like /*/edit which you can then (or should then) map with the SimpleUrlHandlerMapping (for the /places/** ) else you won't be able to configure multiple instances.


        • #5
          It's not entirely clear what you're trying to achieve. Based on what you've said it sounds like one controller with multiple URL patterns could do, i.e. add @RequestMapping(value={"/places", "/events", "/restaurants"}) as a class-level mapping. Or if not provide a more complete example.