1

I'd like to use Jackson to deserialize JSON strings from client requests to Java objects and use FlexJson to serialize Java objects to response.

In the nutshell the issue is: how to setup the Spring to use Jackson ONLY for request handling and not for response?

In servlet-context.xml I have:

<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <beans:property name="messageConverters">
        <beans:list>
            <beans:ref bean="jsonMessageConverter"/>
        </beans:list>
    </beans:property>
</beans:bean>

<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean> 

And in the controller:

@RequestMapping(value = "settings")
public @ResponseBody String getSomeData(@RequestBody UserData userData) { 
    // userData is automatically deserialized by Jackson   
    MyView viewForClient = new MyView(userData);        

    return new JSONSerializer().include(MyView.SERILIZABLE_FIELDS).exclude("*", "*.class").serialize(viewForClient); // here I don't want Jackson to handle the response        
}

But this way Jackson also converts to JSON the response already converted by FlexJSON that I don't want.

Is there any solution? Thanks.

Dmitry B.
  • 99
  • 3
  • 9

1 Answers1

1

You should be able to build a custom MappingJackson2HttpMessageConverter bean where you plug in Jackson for the serialization methods, and FlexJSON for the deserialization methods.

Misha
  • 909
  • 1
  • 7
  • 15
  • 1
    Yes, that could work, however I have already found the better way. Spring has a number of default HttpRequestConverter instances pre-enabled and uses convenient one depending on the request Content-Type. So in my case it was just enough to add Jackson dependency in the project and send request with application/json content type. That causes Jackson to read data from the request body and deserialize to object. And response is not processed by Jackson. – Dmitry B. Jun 09 '14 at 08:22
  • same here, just added the jackson2 jars, and you get to do things like (at)RequestBody YourDomainObject yourDomainObject and jackson does its thing. I imagine if you tried to return yourDomainObject, the reverse would kick into effect with (at)ResponseBody, but if you're using flexjson, then you're most likely returning a ResponseEntity which doesnt seem to trigger any jackson processing – chrismarx Jun 05 '15 at 19:11