2

I want to use custom JsonSerializer for JSON response of SpringMVC4.

In order to add JsonSerializer, I created WebMvcConfigurerAdapter subclass. But customization of MappingJackson2HttpMessageConverter didn't work.

Simplify the problem, I tried setJsonPrefix. But it also didn't work. The response didn't changed.

My code is below. Please tell me what is wrong.

ControllerClass

@Controller
public class SampleController {

    @RequestMapping("/sample")
    @ResponseBody
    public ResponseModel action() {
        return new ResponseModel();
    }

    public static class ResponseModel {
        public String id = "001";
        public String text = "aaa";
    }
}

Configuration

@Configuration
@EnableWebMvc
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(converter());
        super.configureMessageConverters(converters);
    }

    @Bean
    protected MappingJackson2HttpMessageConverter converter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setJsonPrefix("prefix");
        return converter;
    }
}

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
    xmlns:aop="http://www.springframework.org/schema/aop">


    <!-- base package -->
    <context:annotation-config />
    <context:component-scan base-package="jp.co.xxx.*" /><!-- my package. contains WebMvcConfiguration class -->

    <annotation-driven />

    <!-- aop -->
    <aop:aspectj-autoproxy />

</beans:beans>

Note.

  1. When server starts, configureMessageConverters method was called. (Breakpoint confirmation)

  2. I am using AbstractJsonpResponseBodyAdvice subclass for JSONP (I removed this class, but nothing was changed.)

  3. I used below as reference.

How to configure MappingJacksonHttpMessageConverter while using spring annotation-based configuration?

http://www.baeldung.com/spring-httpmessageconverter-rest

  1. SpringMVC version is 4.1.6

P.S. In JSONSerializer case is below.

@Configuration
@EnableWebMvc
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

    @Autowired
    protected CustomObjectMapper mapper;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(converter());
        super.configureMessageConverters(converters);
    }

    @Bean
    protected MappingJackson2HttpMessageConverter converter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(mapper);
        return converter;
    }
}

ObjectMapper

@Component
public class CustomObjectMapper extends ObjectMapper {
    private static final long serialVersionUID = -6987863269632420904L;

    public CustomObjectMapper() {
        setSerializationInclusion(Include.NON_NULL);
        enable(SerializationFeature.INDENT_OUTPUT);

        SimpleModule module = new SimpleModule();
        module.addSerializer(DateTime.class, new DateTimeSerializer());
        registerModule(module);
    }
}

In each case I had no error. But customization didn't work.

Community
  • 1
  • 1
Yamamoto
  • 165
  • 8

2 Answers2

0

In the configureMessageConverters method, if you are not covered and no other converter is added, converters are empty, and WebMvcConfigurationSupport will call addDefaultHttpMessageConverters, which will configure the default converter, which contains the default MappingJackson2HttpMessageConverter.

So adding MappingJackson2HttpMessageConverter in extendMessageConverters will not work.

There are two solutions:

  1. You add the required converter in the configureMessageConverters method itself

  2. To determine the type of converter in extendMessageConverters, set the required properties

sorry,i speek broken english.

0

I was having this problem as well and discovered this problem, thanks to another site:

@EnableWebMvc is equivalent to in XML based configuration.

If you have BOTH, then the extendMessageConverters doesn't seem to be effective. As soon as I removed the XML entry, bingo.. the custom converters started working.

ticktock
  • 1,593
  • 3
  • 16
  • 38