[英]Issue for empty string deserialization
當我調用返回帶有空字符串屬性的對象的Web服務時,Spring 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": "",
"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服務時,將獲得此對象:
{
"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進行自動配置。
在反序列化對象時,如何配置應用程序以保留空字符串?
編輯:我通過向ObjectMapper添加模塊以進行字符串反序列化來嘗試此解決方案 ,但從未調用此方法。
編輯2:在BeanDeserializer類中,在反序列化期間,字段“ popin_type_code”的JsonToken等於JsonToken.VALUE_NULL。 我不明白Spring / Jackson是如何生成這個JsonToken的。
嘗試禁用ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
反序列化功能,但是默認情況下不應啟用該功能,因此如果這是解決方案,我會感到驚訝。
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使用Spring RestTemplate類的默認構造函數。 因此,它使用默認的MessageConverter列表。
解決方案是為我的CustomRestTemplate添加一個構造函數,並使用MessageConverter列表作為輸入。
@Component
public class CustomRestTemplate extends RestTemplate {
@Autowired
public CustomRestTemplate (List<HttpMessageConverter<?>> messageConverters) {
super(messageConverters);
}
}
並配置帶有禁用的“ ACCEPT_EMPTY_STRING_AS_NULL_OBJECT”功能的轉換器:
@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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.