簡體   English   中英

有什么方法可以跳過不兼容的 class 成員的反序列化?

[英]Any way to skip deserialization of class members which are incompatible?

我正在通過 RestTemplate 調用外部 API。 響應結構中有一個參數是動態的,即返回兩種類型的值,成功時返回 Json 結構(status = true),失敗時返回空字符串“”(status = false)。 RestTemplate 在失敗情況下拋出異常,因為字符串無法反序列化到 class object。 這是我的回復 class:

public class Response {
    private boolean status;
    private String message;
    private DynamicClass data;
}
public static class DynamicClass {
    private String id;
    private String createdDate;
}

此調用引發異常:

ResponseEntity<Response> responseEntity = restTemplate.postForEntity(url, httpRequest, Response.class);

例外:

org.springframework.web.client.RestClientException: Error while extracting response for type [class com.example.data.Response] and content type [application/json;charset=utf-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.example.data.Response$DynamicClass` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (''); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.example.data.Response$DynamicClass` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('')
 at [Source: (sun.net.www.protocol.http.HttpURLConnection$HttpInputStream); line: 1, column: 78] (through reference chain: com.example.data.Response["data"])
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:119)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:998)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:981)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:741)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674)
.
.
.
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.example.data.Response$DynamicClass` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (''); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.example.data.Response$DynamicClass` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('')
 at [Source: (sun.net.www.protocol.http.HttpURLConnection$HttpInputStream); line: 1, column: 78] (through reference chain: com.example.data.Response["data"])

如果它不兼容,我有什么辦法可以告訴解析器跳過該成員的反序列化,因為我不需要那個空字符串? 或者這個問題的任何其他解決方法?

  • 選項1::

如果您使用的是 Spring,覆蓋默認配置的最簡單方法是定義一個 ObjectMapper bean 並將其標記為 @Primary:

檢查:: https://www.baeldung.com/spring-boot-customize-jackson-objectmapper

@Bean
@Primary
public ObjectMapper objectMapper() {

    return new ObjectMapper()
      .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
      .configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT,true);

}
  • 選項 2::將響應轉換為字符串並檢查空正文並通過使用 object 映射器將字符串轉換為 class object 來執行必要的操作。

    ResponseEntity responseEntity = restTemplate.postForEntity(url,httpRequest, String.class);

     if(.StringUtils.isBlank(responseEntity;getBody())) { Gson gson = new Gson(). Response res= gson.fromJson(responseEntity,getBody(). Response;class); }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM