简体   繁体   English

SpringMVC Jackson2HttpMessageConverter自定义无效

[英]SpringMVC Jackson2HttpMessageConverter customization doesn't work

I want to use custom JsonSerializer for JSON response of SpringMVC4. 我想对SpringMVC4的JSON响应使用自定义JsonSerializer。

In order to add JsonSerializer, I created WebMvcConfigurerAdapter subclass. 为了添加JsonSerializer,我创建了WebMvcConfigurerAdapter子类。 But customization of MappingJackson2HttpMessageConverter didn't work. 但是自定义MappingJackson2HttpMessageConverter无效。

Simplify the problem, I tried setJsonPrefix. 为简化问题,我尝试了setJsonPrefix。 But it also didn't work. 但这也没有用。 The response didn't changed. 响应没有改变。

My code is below. 我的代码如下。 Please tell me what is wrong. 请告诉我有什么问题。

ControllerClass 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 调度员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. 服务器启动时,将调用configureMessageConverters方法。 (Breakpoint confirmation) (断点确认)

  2. I am using AbstractJsonpResponseBodyAdvice subclass for JSONP (I removed this class, but nothing was changed.) 我正在将AbstractJsonpResponseBodyAdvice子类用于JSONP(我删除了该类,但未进行任何更改。)

  3. I used below as reference. 我在下面用作参考。

How to configure MappingJacksonHttpMessageConverter while using spring annotation-based configuration? 在使用基于Spring注释的配置时如何配置MappingJacksonHttpMessageConverter?

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

  1. SpringMVC version is 4.1.6 SpringMVC版本是4.1.6

PS In JSONSerializer case is below. PS在JSONSerializer中的情况如下。

@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 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. 但是自定义无效。

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. 在configureMessageConverters方法中,如果没有涉及并且没有添加任何其他转换器,则转换器为空,并且WebMvcConfigurationSupport将调用addDefaultHttpMessageConverters,它将配置默认转换器,该转换器包含默认的MappingJackson2HttpMessageConverter。

So adding MappingJackson2HttpMessageConverter in extendMessageConverters will not work. 因此,在extendMessageConverters中添加MappingJackson2HttpMessageConverter将不起作用。

There are two solutions: 有两种解决方案:

  1. You add the required converter in the configureMessageConverters method itself 您将所需的转换器添加到configureMessageConverters方法本身中

  2. To determine the type of converter in extendMessageConverters, set the required properties 要确定extendMessageConverters中的转换器类型,请设置所需的属性

sorry,i speek broken english. 对不起,我发现英语不好。

I was having this problem as well and discovered this problem, thanks to another site: 由于另一个站点,我也遇到了这个问题,并发现了这个问题:

@EnableWebMvc is equivalent to in XML based configuration. @EnableWebMvc等效于基于XML的配置。

If you have BOTH, then the extendMessageConverters doesn't seem to be effective. 如果您同时拥有,则extendMessageConverters似乎无效。 As soon as I removed the XML entry, bingo.. the custom converters started working. 删除XML条目后,bingo ..自定义转换器就开始工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM