spring config custom httpMessageConverter

in spring 4.1.2, the GsonHttpMessageConverter is added so that we could use it to replace the jackson converter for json serialization and deserialization.

To added it, we could config our own RequestMappingHandlerAdapter and add the gson one to the “messageConverters” field. Also we would have to add bean validator for the RequestMappingHandlerAdapter. By doing this, i got several validation error while loading spring context when i start my Tomcat saying type mismatch. The mismatch happens in those xml configed setter for java properties like “velocityProperties” for “VelocityEngineFactoryBean”

	<bean id="myVelocityEngine"
		class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
		<property name="velocityProperties">
			<value>
				resource.loader = string
				string.resource.loader.class = org.apache.velocity.runtime.resource.loader.StringResourceLoader
			</value>
		</property>
	</bean>

It complains that the content of “velocityProperties” does not match the java properties. I then change to use the “props -> prop” pattern in xml and also fails. Of course i can avoid this buy config it in the java code.

However The easiest way i found it to added it thru the mvc:annotation-driven.

   <mvc:annotation-driven>
      <mvc:message-converters>
         <bean class="myOrg.ui.server.controller.converter.ExtendedGsonHttpMessageConverter"/>
      </mvc:message-converters>
   </mvc:annotation-driven>

This way we use the namespace-> mvc:annotation-driven to create the HandlerMapping and HandlerAdapter for us. Also we could pass in the desired converter thru the mvc:message-converters.

If you prefer java based config. reference here. We could leverage the ‘WebMvcConfigurerAdapter’ and override the ‘configureMessageConverters()’ to add the GsonHttpMessageConverter.

Here i use the ExtendedGsonHttpMessageConverter because i need it to be able to properly handle my Date format to de/serialize Date.

public class ExtendedGsonHttpMessageConverter extends GsonHttpMessageConverter
{
    protected static final String DATE_FORMAT = "yyyy-MM-dd";

    public ExtendedGsonHttpMessageConverter()
    {
        super();
        super.setGson(buildGson());
    }
    protected static Gson buildGson() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        /**
         * set the time format so that new Date Adapters will be created and added to handle 3 Date types: Date, sqlDate and TimpeStamp.
         * These adapters will be at the front of the default adapters so that date will be handled by them.
         */
        gsonBuilder.setDateFormat(DATE_FORMAT);
        return gsonBuilder.create();
    }
}

More Detail here

Advertisements

2 comments

  1. vivek sinha · February 10, 2016

    Thanks for a great post! It really helped me!

    But I’ve a query, as I want to serialize the object depending on the where this request came from. As Mobile app and website will have different serialization policy. How to achieve that?

    • LEON · February 10, 2016

      I am not sure how ur app is architected. Do you happen to have or you can add some indicator to identify the request source? Then you can apply different serialization/deserilazation accordingly. I personally do no know any Spring-builtIn way of doing that.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s