In Spring 3.x the AnnotationMethodHandlerAdapter class is extended to support the @RequestBody parameter and has the following HttpMessageConverters registered by default:
- ByteArrayHttpMessageConverter converts byte arrays
- StringHttpMessageConverter converts strings
- FormHttpMessageConverter converts form data to/from a MultiValueMap<String, String>
- SourceHttpMessageConverter converts to/from a javax.xml.transform.Source
- MarshallingHttpMessageConverter converts to/from an object using the org.springframework.oxm package
The following snippet shows the Spring configuration required to iterate over the message converters sequentially until it finds one that matches the object you are returning, so it can render JSON objects from the form controller receiving the data:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<bean id="messageAdapter" | |
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> | |
<property name="messageConverters"> | |
<list> | |
<!-- Message converters --> | |
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/> | |
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/> | |
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /> | |
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/> | |
<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/> | |
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> | |
</list> | |
</property> | |
</bean> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RequestMapping(method = RequestMethod.POST) | |
protected ModelAndView updateContactList(HttpServletRequest request, | |
HttpServletResponse response, | |
@RequestBody ContactUpdateList contactList) | |
throws Exception | |
{ | |
String listOwner = contactList.getOwner(); | |
ContactList theList = contactList.getContactList(); | |
... | |
} |
This post could perfectly finish at this point if you were only interested in processing JSON objects of the same type, but if that is not the case and your Spring application needs to use other types of converters keep reading.
By default the Spring MVC defines three different request handler adapters, they are:
- org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
- org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
- org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
Since we are creating the AnnotationMethodHandlerAdapter in our example context definition above, Spring will not create the other two handler adapters.
In case you need to use other types of adapters, you will have to specifically create the beans for those adapters, as shown next:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> | |
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean> | |
<bean id="messageAdapter" | |
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> | |
<property name="messageConverters"> | |
<list> | |
<!-- Message converters --> | |
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/> | |
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/> | |
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /> | |
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/> | |
<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/> | |
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> | |
</list> | |
</property> | |
</bean> |
No comments:
Post a Comment