Question

My understanding is that Spring MVC ships with a version/dependency of the Jackson JSON library. I really like/prefer FlexJSON and was wondering if it is possible to configure Spring to use FlexJSON instead, and if so, how?

Was it helpful?

Solution

Spring uses the HttpMessageConverter to generate a response when you annotate your handle method with @ResponseBody or when you return a ResponseEntity object.

When Spring sets up your MVC configuration, it registers a number of HttpMessageConverter implementations, among which is the Jackson implementation, MappingJackson2HttpMessageConverter. It does this in the WebMvcConfigurationSupport class (Spring MVC 3.1+)

@SuppressWarnings("deprecation")
protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
    StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
    stringConverter.setWriteAcceptCharset(false);

    messageConverters.add(new ByteArrayHttpMessageConverter());
    messageConverters.add(stringConverter);
    messageConverters.add(new ResourceHttpMessageConverter());
    messageConverters.add(new SourceHttpMessageConverter<Source>());
    messageConverters.add(new AllEncompassingFormHttpMessageConverter());
    if (romePresent) {
        messageConverters.add(new AtomFeedHttpMessageConverter());
        messageConverters.add(new RssChannelHttpMessageConverter());
    }
    if (jaxb2Present) {
        messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
    }
    if (jackson2Present) {
        messageConverters.add(new MappingJackson2HttpMessageConverter());
    }
    else if (jacksonPresent) {
        messageConverters.add(new org.springframework.http.converter.json.MappingJacksonHttpMessageConverter());
    }
}

These are registered as defaults. Before doing this, Spring registers any custom HttpMessageConverter bean or instances you might want.

So the solution is to implement your own with flexjson and register it. Registering depends on what kind of configuration you are doing, but there are resources out there for each.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top