自定义Jackson解串器未在春季注册 [英] Custom Jackson deserializer does not get registered in Spring

查看:78
本文介绍了自定义Jackson解串器未在春季注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在春季用Jackson来反序列化一些JSON. 我已将其配置为使用自定义MappingJackson2HttpMessageConverter,我可以看到它已注册. 仍然,当尝试反序列化时,我看到它没有使用我的自定义MappingJackson2HttpMessageConverter,而是使用了HttpMessageConverters的defaultList(换句话说,它没有被覆盖). 知道如何解决吗?谢谢.

I'm trying to deserialize some JSON with Jackson in Spring. I have configured it to use a custom MappingJackson2HttpMessageConverter and I can see it gets registered. Still, when trying to deserialize, I see that it is not using my custom MappingJackson2HttpMessageConverter but that instead it uses the defaultList of HttpMessageConverters (in other words it's not overridden). Any idea how it could be solved ? Thanks.

package com.bigid.scanner.service.restapi;

import com.bigid.scanner.api.datalink.DataLink;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.text.SimpleDateFormat;
import java.util.List;

@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.mixIn(DataLink.class, PolymorphicDataLinkMixin.class);
        converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
    }
}

推荐答案

感谢您的帮助.

问题出在我身上 web.xml文件中的<mvc: annotation-driven>. 因此,messageConverters将被注册两次:

Problem was I had <mvc: annotation-driven> inside the web.xml file. Because of that, the messageConverters would get registered two times:

  • 使用默认messageConverters(总共9个)一次
  • 根据我的代码,该代码只会注册一个.

虽然使用@RequestBody处理值,但它们将被默认值(9个messageConverters)而不是我的序列化(不确定这是否是随机行为,但是在任何情况下都会产生意外结果).

While processing values with @RequestBody, they would get serialized by the default one (9 messageConverters) and not by mine (not sure if this is random behavior, but any case it would have unexpected results).

希望它可以帮助某人!

Hope it helps someone !

这篇关于自定义Jackson解串器未在春季注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆