【问题标题】:SpringMVC Jackson2HttpMessageConverter customization doesn't workSpringMVC Jackson2HttpMessageConverter 自定义不起作用
【发布时间】:2015-09-23 17:09:02
【问题描述】:

我想为 SpringMVC4 的 JSON 响应使用自定义 JsonSerializer。

为了添加 JsonSerializer,我创建了 WebMvcConfigurerAdapter 子类。 但是 MappingJackson2HttpMessageConverter 的自定义不起作用。

简化问题,我试过setJsonPrefix。 但它也没有奏效。响应没有改变。

我的代码如下。请告诉我有什么问题。

控制器类

@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
@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>

注意。

  1. 当服务器启动时,configureMessageConverters 方法被调用。 (断点确认)

  2. 我正在为 JSONP 使用 AbstractJsonpResponseBodyAdvice 子类 (我删除了这个类,但没有任何改变。)

  3. 我用下面作为参考。

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

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

  1. SpringMVC 版本为 4.1.6

附: 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;
    }
}

对象映射器

@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);
    }
}

在每种情况下,我都没有错误。但是自定义不起作用。

【问题讨论】:

  • 你在哪里用的,能不能把应该修改的错误日志或者响应贴一下。
  • 您是否尝试过删除配置中的@EnableWebMvc?
  • 我尝试删除@EnableWebMvc。在那种情况下,我使用了 WebMvcConfigurationSupport 而不是 WebMvcConfigurerAdapter。
  • 以上代码没有使用 JSONSerializer。实际上,我将 CustomObjectMapper 设置为 MappingJackson2HttpMessageConverter。上述代码的响应是 {"id":"001","text":"aaa"}。我没有错误。但是响应没有 JsonPrefix。
  • 我使用 JSONSerializer 添加了代码。

标签: java spring spring-mvc jackson


【解决方案1】:

在configureMessageConverters方法中,如果没有覆盖到没有添加其他转换器,转换器为空,WebMvcConfigurationSupport会调用addDefaultHttpMessageConverters,会配置默认转换器,其中包含默认的MappingJackson2HttpMessageConverter。

所以在extendMessageConverters中添加MappingJackson2HttpMessageConverter是行不通的。

有两种解决方案:

  1. 您在 configureMessageConverters 方法本身中添加所需的转换器

  2. 要确定extendMessageConverters中转换器的类型,设置所需的属性

对不起,我的英语不好。

【讨论】:

    【解决方案2】:

    我也遇到了这个问题,发现了这个问题,感谢另一个网站:

    @EnableWebMvc 等价于基于 XML 的配置。

    如果两者都有,则 extendMessageConverters 似乎无效。删除 XML 条目后,宾果游戏.. 自定义转换器开始工作。

    【讨论】:

      猜你喜欢
      • 2012-05-05
      • 2020-05-21
      • 2019-11-13
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 2023-03-03
      • 1970-01-01
      相关资源
      最近更新 更多