Wednesday, February 6, 2013

Spring register a new JSON Converter

Yesterday I had to modify the JSON during creation from an object. I want to add validation hints along with the json that travelled to the server.  Spring has converters and uses MappingJacksonHttpMessageConverter to convert the object returned by the controller. SO i want to modify the converter to include the hints in the JSON generated.

A lot of googling happened for no luck finally I got the solution which reads as follows

First override the default handler: MappingJsonHttpMessageConverter

public class JSONHttpMessageConverter extends MappingJacksonHttpMessageConverter {

protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException,
HttpMessageNotWritableException {

now in spring xml modify the mvc:annotation-config; Please make sure that u dont have another annotation-config with in any of the spring xmls in ur classpath.

    <mvc:annotation-driven  validator="ExtendedValidatationAdaptor" >
<mvc:message-converters register-defaults="true" >
<bean class="" p:somebean-ref="somref" />
and this should do. 
Tested with spring-web-3.1.1.RELEASE.jar

No comments:

Post a Comment