Announcement Announcement Module
Collapse
No announcement yet.
Is consumes supposed to disambiguate request mappings? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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?

      Comment


      • #4
        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.

        Comment


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

          Thanks Marten.

          Comment


          • #6
            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).

            Comment


            • #7
              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, 09:33 AM. Reason: Added doc refs

              Comment


              • #8
                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, 03:00 AM.

                Comment

                Working...
                X