空字符串反序列化的问题 [英] Issue for empty string deserialization

查看:118
本文介绍了空字符串反序列化的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调用Web服务返回一个带有空字符串属性的对象时,Spring projet出现问题.

I have an issue in my Spring projet when I call a web-service that return an object with a attribute that is an empty string.

在我的项目中,我具有Spring Boot 1.5.2,Spring 4.3.7和Jackson 2.8.7.

In my projet I have Spring boot 1.5.2, Spring 4.3.7 and Jackson 2.8.7.

我使用RestTemplate调用Web服务.

I use a RestTemplate to call web-services.

ResponseEntity<T> responseEntity = restTemplate.exchange("web-service-HttpMethod.GET, null, MyObject.Class);
return responseEntity.getBody();

如果我在浏览器中调用Web服务,它将返回以下响应:

If I call the web-service in browser, it returns this response :

{
  "display_item_code": "NEP054",
  "historic": false,
  "popin_type_code": "",
  "combo_box": false,
  "max_combo_box_elements": 0,
  "data_max_length": 0,
  "data_precision": 0,
  "data_min_length": 0,
  "data_control_type_code": "",
  "data_control_value1": "",
  "data_control_value2": "",
  "data_format": "MAJUS",
  "translatable": false,
  "translation_key_type_code": "",
  "default_value_setting": "",
  "default_value": "",
  "text_area": false,
  "family_code": "",
  "popin": null,
  "combo_values": null
}

这是预期的结果. 但是,当我在应用程序中调用此Web服务时,将获得此对象:

That is the expected result. But when I call this web-service in my application, I obtain this object :

{
  "display_item_code": "NEP054",
  "historic": false,
  "popin_type_code": null,
  "combo_box": false,
  "max_combo_box_elements": 0,
  "data_max_length": 0,
  "data_precision": 0,
  "data_min_length": 0,
  "data_control_type_code": null,
  "data_control_value1": null,
  "data_control_value2": null,
  "data_format": "MAJUS",
  "translatable": false,
  "translation_key_type_code": null,
  "default_value_setting": null,
  "default_value": null,
  "text_area": false,
  "family_code": null,
  "popin": null,
  "combo_values": null
}

所有具有空值的属性现在都为空. 我认为有一些要配置的东西,也许是ObjectMapper或JsonParser,但我找不到要做什么. 当前,我使用默认的序列化器,ObjectMapper和JsonParser. 我让Spring Boot进行自动配置.

All the attributes that have an empty value are now null. I think there is something to configure, maybe an ObjectMapper or a JsonParser, but I don't find what to do. Currently I use the default Serializer, ObjectMapper and JsonParser. I let Spring Boot do the autoconfiguration.

我如何配置应用程序以反序列化对象时保留空字符串?

How can I configure my application to keep empty string when it deserialize an object ?

我尝试了

EDIT : I tried this solution by adding a module to the ObjectMapper for string deserialization, but this method is never called.

在反序列化期间的BeanDeserializer类中,字段"popin_type_code"的JsonToken等于JsonToken.VALUE_NULL. 我不明白Spring/Jackson是如何生成此JsonToken的.

EDIT 2 : In the BeanDeserializer class, during the deserialization, the JsonToken for the field "popin_type_code" is equal to JsonToken.VALUE_NULL. I don't understand how Spring/Jackson generate this JsonToken.

推荐答案

我终于找到了我的问题所在.

I finally found what was my issue.

在我的应用程序中,我使用自定义的RestTemplate.但是此CustomRestTemplate使用Spring RestTemplate类的默认构造函数.因此,它使用默认的MessageConverter列表.

In my application, I use a custom RestTemplate. But this CustomRestTemplate use the default constructor of the Spring RestTemplate class. So it use the default MessageConverter list.

解决方案是为我的CustomRestTemplate添加一个构造函数,并使用MessageConverter列表作为输入.

The solution was to add a constructor for my CustomRestTemplate with the MessageConverter list as input.

@Component
public class CustomRestTemplate extends RestTemplate {
    @Autowired
    public CustomRestTemplate (List<HttpMessageConverter<?>> messageConverters) {
        super(messageConverters);
    }
}

并配置具有禁用的"ACCEPT_EMPTY_STRING_AS_NULL_OBJECT"功能的转换器:

And to configuration the converter with the disabled "ACCEPT_EMPTY_STRING_AS_NULL_OBJECT" feature :

@Configuration
@ComponentScan(basePackages = "com.geodis.rt")
public class WebApplicationConfig extends WebMvcConfigurerAdapter {

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

    @Bean
    MappingJackson2HttpMessageConverter converter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.getObjectMapper().disable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
        return converter;
    }

}

这篇关于空字符串反序列化的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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