Results 1 to 5 of 5

Thread: Multiple instances of annotated Controller with RequestMapping

  1. #1
    Join Date
    Dec 2008
    Posts
    15

    Default 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:
    Code:
    @Controller
    public class SearchController {
    
        private SearchService searchService;
    
        public SearchController(earchService searchService) {
            this.searchService = searchService;
        }
    
        @RequestMapping(value="/search")
        public ResponseEntity<Response> handle(SearchCommand command) {
                // Search code
                searchService.search(...);
    
                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 (http://static.springsource.org/sprin...pping-31-vs-30) that this is no longer possible with the Spring MVC 3.1 way, so what's the alternative?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Dec 2008
    Posts
    15

    Default

    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. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •