Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-7504

Make it easier to add new Message Converters to AnnotationMethodHandlerAdapter

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 3.0.4
    • Fix Version/s: 3.1 M1
    • Component/s: Web
    • Labels:
      None

      Description

      See https://support.springsource.com/spring_support_client_getIncidentById/9995

      I needed to use MarshallingHttpMessageConverter (so @RequestBody could bind a POST of XML to a POJO). AnnotationMethodHandlerAdapter is preconfigured in its constructor with several MessagConverters, but not MarshallingHttpMessageConverter. Using the <mvc:annotation-config/> tag, it was nearly impossible to add this MessageConverter to the AMHA. Per Spring Support, I had to write a BeanPostProcessor to look for the AMHA and then add the MTHC (and to add insult to injury, it was an array!).

      There should be a simpler way to add MessageConverters; in fact, if they are found in the application context they should be added automatically, or they should be added via the mvc:annotation-config. Or anything more elegant than the BeanPostProcessor.

      Also section 19.9 of the reference document implies that MarshallingHttpMessageConverter is configured by default, but it's not.

        Issue Links

          Activity

          kdelong Kenneth DeLong created issue -
          cbeams Chris Beams made changes -
          Field Original Value New Value
          Fix Version/s 3.1 M1 [ 11378 ]
          Platform Parent values: JBoss(10024). Level 1 values: 4.2(10165). Parent values: JBoss(10024).
          Original Estimate 0d [ 0 ]
          Remaining Estimate 0d [ 0 ]
          Virtual Machine Parent values: Sun JVM(10011). Level 1 values: 1.6(10174). Parent values: Sun JVM(10011).
          tmarshall Trevor Marshall made changes -
          Workflow jira [ 33353 ] SPR Workflow [ 40931 ]
          olivergierke Oliver Gierke made changes -
          Assignee Oliver Gierke [ olivergierke ]
          olivergierke Oliver Gierke made changes -
          Status Open [ 1 ] In Progress [ 3 ]
          Hide
          olivergierke Oliver Gierke added a comment -

          Added proposed patch (Git) to be reviewed by Arjen. I added a setter for custom HttpMessageConverter<?> that will be prepended to the default ones, just like you already can do with custom WebArgumentResolver.

          Show
          olivergierke Oliver Gierke added a comment - Added proposed patch (Git) to be reviewed by Arjen. I added a setter for custom HttpMessageConverter<?> that will be prepended to the default ones, just like you already can do with custom WebArgumentResolver .
          olivergierke Oliver Gierke made changes -
          Attachment 7504.patch [ 17101 ]
          olivergierke Oliver Gierke made changes -
          Status In Progress [ 3 ] Open [ 1 ]
          olivergierke Oliver Gierke logged work - 14/Sep/10 6:59 AM
          • Time Spent:
            0.5h
             
            <No comment>
          olivergierke Oliver Gierke made changes -
          Time Spent 0.5h [ 1800 ]
          Hide
          olivergierke Oliver Gierke added a comment -

          Reassigned for review and/or further ideas.

          Show
          olivergierke Oliver Gierke added a comment - Reassigned for review and/or further ideas.
          olivergierke Oliver Gierke made changes -
          Assignee Oliver Gierke [ olivergierke ] Arjen Poutsma [ arjen.poutsma ]
          rstoya05-aop Rossen Stoyanchev made changes -
          Link This issue is related to SPR-7091 [ SPR-7091 ]
          rstoya05-aop Rossen Stoyanchev made changes -
          Link This issue is related to SPR-7191 [ SPR-7191 ]
          Hide
          rstoya05-aop Rossen Stoyanchev added a comment -

          It is now possible to specify a list of HttpMessageConverters through the MVC namespace:

          <mvc:annotation-driven>
              <mvc:message-converters>
                  <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
                  <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
                  <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
              </mvc:message-converters>
          </mvc:annotation-driven>

          This option overrides the default set of HttpMessageConverters. Hence when specified the list must include all required message converters.

          Show
          rstoya05-aop Rossen Stoyanchev added a comment - It is now possible to specify a list of HttpMessageConverters through the MVC namespace: < mvc :annotation-driven> < mvc :message-converters> < bean class = "org.springframework.http.converter.StringHttpMessageConverter" /> < bean class = "org.springframework.http.converter.ResourceHttpMessageConverter" /> < bean class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> </ mvc :message-converters> </ mvc :annotation-driven> This option overrides the default set of HttpMessageConverters. Hence when specified the list must include all required message converters.
          rstoya05-aop Rossen Stoyanchev made changes -
          Resolution Complete [ 8 ]
          Status Open [ 1 ] Resolved [ 5 ]
          Assignee Arjen Poutsma [ arjen.poutsma ] Rossen Stoyanchev [ rstoya05-aop ]
          Hide
          jhuxhorn Joern Huxhorn added a comment -

          I tried to use the above with the current 3.1.0.BUILD-SNAPSHOT but to no avail...
          Starting up the application causes a
          org.xml.sax.SAXParseException: cvc-complex-type.2.1: Element 'mvc:annotation-driven' must have no character or element information item [children], because the type's content type is empty.

          I assume that using the 3.0 schemas is causing this issue but I couldn't find the location of the new 3.1 schemas anywhere.
          Any idea how to work around this?

          Show
          jhuxhorn Joern Huxhorn added a comment - I tried to use the above with the current 3.1.0.BUILD-SNAPSHOT but to no avail... Starting up the application causes a org.xml.sax.SAXParseException: cvc-complex-type.2.1: Element 'mvc:annotation-driven' must have no character or element information item [children] , because the type's content type is empty. I assume that using the 3.0 schemas is causing this issue but I couldn't find the location of the new 3.1 schemas anywhere. Any idea how to work around this?
          Hide
          rstoya05-aop Rossen Stoyanchev added a comment -

          The schemas are in the jar. They don't need to be available at the URI specified in the schema location. Just make sure your schema location in the <beans> element points to spring-mvc.xsd (or spring-mvc-3.1.xsd) and not spring-mvc-3.0.xsd.

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
           
          </beans>

          Show
          rstoya05-aop Rossen Stoyanchev added a comment - The schemas are in the jar. They don't need to be available at the URI specified in the schema location. Just make sure your schema location in the <beans> element points to spring-mvc.xsd (or spring-mvc-3.1.xsd) and not spring-mvc-3.0.xsd. <? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:mvc = "http://www.springframework.org/schema/mvc" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">   </ beans >
          Hide
          jhuxhorn Joern Huxhorn added a comment -

          Thank you very much for this fast and accurate response!
          Everything is working fine now.

          Show
          jhuxhorn Joern Huxhorn added a comment - Thank you very much for this fast and accurate response! Everything is working fine now.
          Hide
          jkuipers Joris Kuipers added a comment -

          Instead of always overriding the list of default converters, forcing people to know what the defaults are exactly so that they can list them again if they only want to register one or two extra converters, why don't we add an attribute like "appendToDefaults" to the message-converters element?
          In general I would like to see Spring to offer more support for keeping defaults, as very often more items are added to the default list of mvc-related components with new framework versions and I don't always want to have to keep track of that as I update my applications to benefit from them (Formatters, Converters, HttpMessageConverters, HandlerMappings, HandlerAdapters, etc). Also, the defaults sometimes include smart classpath checking for things like Jackson and Rome, which are not available to me as a simple user using this namespace: being able to append to the defaults would be much simpler.

          Show
          jkuipers Joris Kuipers added a comment - Instead of always overriding the list of default converters, forcing people to know what the defaults are exactly so that they can list them again if they only want to register one or two extra converters, why don't we add an attribute like "appendToDefaults" to the message-converters element? In general I would like to see Spring to offer more support for keeping defaults, as very often more items are added to the default list of mvc-related components with new framework versions and I don't always want to have to keep track of that as I update my applications to benefit from them (Formatters, Converters, HttpMessageConverters, HandlerMappings, HandlerAdapters, etc). Also, the defaults sometimes include smart classpath checking for things like Jackson and Rome, which are not available to me as a simple user using this namespace: being able to append to the defaults would be much simpler.
          Hide
          rstoya05-aop Rossen Stoyanchev added a comment -

          What we could do is place user-provided message converters in front of the default ones. So whether your provide a completely new JSON converter or an instance of say MappingJacksonHttpMessageConverter configured in a slightly different way, in both cases your converter would override or take precedence over the default ones.

          With this there needs to be a way to turn off default message converter registrations entirely. That we could do through a "register-defaults" attribute on the <mvc:message-converters> element, which would be set to true by default.

          Show
          rstoya05-aop Rossen Stoyanchev added a comment - What we could do is place user-provided message converters in front of the default ones. So whether your provide a completely new JSON converter or an instance of say MappingJacksonHttpMessageConverter configured in a slightly different way, in both cases your converter would override or take precedence over the default ones. With this there needs to be a way to turn off default message converter registrations entirely. That we could do through a "register-defaults" attribute on the <mvc:message-converters> element, which would be set to true by default.
          Hide
          jkuipers Joris Kuipers added a comment -

          Sounds like a good idea to me!

          Show
          jkuipers Joris Kuipers added a comment - Sounds like a good idea to me!
          cbeams Chris Beams made changes -
          Status Resolved [ 5 ] Closed [ 6 ]
          tmarshall Trevor Marshall made changes -
          Workflow SPR Workflow [ 40931 ] New SPR Workflow [ 58639 ]
          tmarshall Trevor Marshall made changes -
          Workflow New SPR Workflow [ 58639 ] SPR Workflow [ 68242 ]
          juergen.hoeller Juergen Hoeller made changes -
          Link This issue is related to SPR-7904 [ SPR-7904 ]
          Transition Time In Source Status Execution Times Last Executer Last Execution Date
          Open Open In Progress In Progress
          15d 11h 50m 1 Oliver Gierke 14/Sep/10 6:54 AM
          In Progress In Progress Open Open
          4m 11s 1 Oliver Gierke 14/Sep/10 6:58 AM
          Open Open Resolved Resolved
          133d 3h 26m 1 Rossen Stoyanchev 25/Jan/11 10:25 AM
          Resolved Resolved Closed Closed
          510d 17h 18m 1 Chris Beams 19/Jun/12 3:44 AM

            People

            • Assignee:
              rstoya05-aop Rossen Stoyanchev
              Reporter:
              kdelong Kenneth DeLong
              Last updater:
              Juergen Hoeller
            • Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                6 years, 43 weeks ago

                Time Tracking

                Estimated:
                Original Estimate - 0d
                0d
                Remaining:
                Remaining Estimate - 0d
                0d
                Logged:
                Time Spent - 0.5h
                0.5h