Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 空字符串反序列化问题_Java_Json_Spring_Jackson - Fatal编程技术网

Java 空字符串反序列化问题

Java 空字符串反序列化问题,java,json,spring,jackson,Java,Json,Spring,Jackson,当我调用返回属性为空字符串的对象的web服务时,我的Spring projet中有一个问题 在我的projet中,我有Spring boot 1.5.2、Spring 4.3.7和Jackson 2.8.7 我使用RestTemplate调用web服务 ResponseEntity<T> responseEntity = restTemplate.exchange("web-service-HttpMethod.GET, null, MyObject.Class); return r

当我调用返回属性为空字符串的对象的web服务时,我的Spring projet中有一个问题

在我的projet中,我有Spring boot 1.5.2、Spring 4.3.7和Jackson 2.8.7

我使用RestTemplate调用web服务

ResponseEntity<T> responseEntity = restTemplate.exchange("web-service-HttpMethod.GET, null, MyObject.Class);
return responseEntity.getBody();
这是预期的结果。 但当我在应用程序中调用此web服务时,我获得以下对象:

{
  "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
}
所有具有空值的属性现在都为null。 我认为有一些东西需要配置,可能是ObjectMapper或JsonParser,但我找不到该做什么。 目前我使用默认的序列化程序、ObjectMapper和JsonParser。 我让Spring Boot进行自动配置

当我的应用程序反序列化一个对象时,我如何配置它以保持空字符串

编辑:我尝试向ObjectMapper添加一个模块以进行字符串反序列化,但从未调用此方法

编辑2:在BeanDeserializer类中,在反序列化过程中,字段“popin_type_code”的JsonToken等于JsonToken.VALUE_NULL。
我不明白Spring/Jackson是如何生成这个JsonToken的。

尝试禁用
接受空字符串作为空字符串对象
反序列化功能,但是默认情况下不应该启用它,所以如果这是解决方案,我会感到惊讶

import com.fasterxml.jackson.databind.DeserializationFeature;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

@Configuration
public class JacksonConfiguration {

    @Bean
    public Jackson2ObjectMapperBuilder jacksonBuilder() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();

        builder.featuresToDisable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

        return builder;
    }
}

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

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

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

@Component
public class CustomRestTemplate extends RestTemplate {
    @Autowired
    public CustomRestTemplate (List<HttpMessageConverter<?>> messageConverters) {
        super(messageConverters);
    }
}
@组件
公共类CustomRestTemplate扩展了RestTemplate{
@自动连线
公共CustomRestTemplate(列表>转换器){
添加(0,converter());
}
@豆子
MappingJackson2HttpMessageConverter转换器(){
MappingJackson2HttpMessageConverter=新的MappingJackson2HttpMessageConverter();
converter.getObjectMapper().disable(反序列化功能。接受\u空\u字符串\u作为\u空\u对象);
回流转换器;
}
}

可能重复我刚刚尝试了这个解决方案,但没有解决我的问题。我希望你将它切换到了相反的方向,因为你想要它,反之亦然。当然,我切换到相反的方向。我不认为使用默认配置,所以我猜你的想法是正确的。我只是尝试了这个解决方案,但它没有解决我的问题。当BeanDeserializer反序列化字段“popin_type_code”时,JsonToken等于JsonToken.VALUE_NULL。
@Component
public class CustomRestTemplate extends RestTemplate {
    @Autowired
    public CustomRestTemplate (List<HttpMessageConverter<?>> messageConverters) {
        super(messageConverters);
    }
}
@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;
    }

}