Results 1 to 8 of 8

Thread: Is consumes supposed to disambiguate request mappings?

  1. #1
    Join Date
    Jun 2012
    Posts
    3

    Default Is consumes supposed to disambiguate request mappings?

    I have two request mappings in a Spring MVC 3 application, one which takes json and xml, and another that takes application/x-www-form-urlencoded data. Example:

    Code:
    @RequestMapping(value={"/v1/foos"}, method = RequestMethod.POST, consumes={"application/json", "application/xml"})
    public FooDTO createFoo(@RequestBody FooDTO requestDTO) throws Exception {
        ...
    }
    
    @RequestMapping(value={"/v1/foos"}, method = RequestMethod.POST, consumes="application/x-www-form-urlencoded")
    public FooDTO createFooWithForm(@ModelAttribute FooDTO requestDTO) throws Exception {
        ...
    }
    I expected that the different consumes parameter makes each request unique, though I get an java.lang.IllegalStateException: Ambiguous handler methods mapped....

    Should consumes and produces makes requests unique? Any ideas?

    To add weight to this, if you set the content-type in the header rather than using consumes, this actually works and makes them unique: headers="content-type=application/x-www-form-urlencoded. Perhaps there is a bug with consumes?

    NOTE: We're using Spring 3.1.1.RELEASE.

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

    Default

    It should however it also depends on your configuration and what you use. It might be that if you use the DefaultAnnotationHandlerMapping that it doesn't work because you should use the RequestMappingHandlerMapping instead.
    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
    Jun 2012
    Posts
    3

    Default

    Thanks for your response Marten.

    I was in fact using the DefaultAnnotationHandlerMapping, I have changed this to RequestMappingHandlerMapping and I'm still getting this problem.

    I can confirm that the following works:

    headers="content-type=application/xml"
    headers="content-type=application/json"

    But the following causes ambiguous mappings:

    consumes="application/xml"
    consumes="application/json"

    I believe that these should work in the same way. Is this correct? If so then is it a bug?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You should change both the HandlerMapping as well as the HandlerAdapter (use the RequestMappingHandlerAdapter).

    In theory it should work if it doesn't feel free to register an issue.
    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
    Jun 2012
    Posts
    3

    Default

    You hit the nail on the head there Marten, that seems to have fixed it.

    Thanks Marten.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    One of those nifty things of the new RequestMapping stuff in spring 3.1. There is some fine print stating that the old @RequestMapping stuff won't receive any new features (which explains why the headers attribute was working and the consumes wasn't).
    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

  7. #7
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    37

    Default

    Can somebody please elaborate on this? What are the differencies between RequestMappingHandlerMapping and DefaultAnnotationHandlerMapping? I understood that the former is able to manage the "consume" property, and that the latter in not and "won't receive any new features".

    I also know that the DefaultAnnotationHandlerMapping is instantiated by default in the DispatcherServlet. Does this means that by default we are using "deprecated" code? Of course these are just my early thoughts and I'd love any clarification on this, I'm reading the docs but I'm not sure why and when should I use these new classes.

    Thanks a lot.

    EDIT
    Just a bit after writing this post I came to the relevant part in the documentation

    http://static.springsource.org/sprin.../html/mvc.html

    16.3.2.1 New Support Classes for @RequestMapping methods in Spring MVC 3.1

    Spring 3.1 introduced a new set of support classes for @RequestMapping methods called RequestMappingHandlerMapping and RequestMappingHandlerAdapter respectively. They are recommended for use and even required to take advantage of new features in Spring MVC 3.1 and going forward. The new support classes are enabled by default by the MVC namespace and MVC Java config (@EnableWebMvc) but must be configured explicitly if using neither. This section describes a few important differences between the old and the new support classes.
    Last edited by namero999; Jun 24th, 2012 at 08:33 AM. Reason: Added doc refs

  8. #8
    Join Date
    Jun 2012
    Posts
    1

    Default

    Quote Originally Posted by namero999 View Post
    Can somebody please elaborate on this? What are the differencies between RequestMappingHandlerMapping and DefaultAnnotationHandlerMapping? I understood that the former is able to manage the "consume" property, and that the latter in not and "won't receive any new features".

    I also know that the DefaultAnnotationHandlerMapping is instantiated by default in the DispatcherServlet. Does this means that by default we are using "deprecated" code? Of course these are just my early thoughts and I'd love any clarification on this, I'm reading the docs but I'm not sure why and when should I use these new classes.

    Thanks a lot.

    EDIT
    Just a bit after writing this post I came to the relevant part in the documentation

    http://static.springsource.org/sprin.../html/mvc.html
    Is there anyone know this?
    Last edited by Johnho; Nov 12th, 2012 at 02:00 AM.

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
  •