2013-02-03 198 views
0

webservice返回一個空字符串,而不是NULL,導致Jackson崩潰。 因此,我創建了一個自定義分析器,我試圖手動解析它?任何想法我怎麼能做到這一點?RestTemplate&Jackson - 自定義JSON反序列化?

我在這裏做錯了什麼?我想要做的就是像往常一樣將JSON解析爲對象。字段名稱使用@JsonProperty添加到我的屬性中,因此解析器應該知道如何轉換它。

public class InsertReplyDeserializer extends JsonDeserializer<ListingReply> { 

    @Override 
    public ListingReply deserialize(JsonParser jsonParser, DeserializationContext arg1) 
      throws IOException, JsonProcessingException { 

     ObjectCodec oc = jsonParser.getCodec(); 
     JsonNode node = oc.readTree(jsonParser); 

     // If service returns "" instead of null return a NULL object and don't try to parse 
     if (node.getValueAsText() == "") 
      return null; 


     ObjectMapper objectMapper = new ObjectMapper(); 
     ListingReply listingReply = objectMapper.readValue(node, ListingReply.class); 


     return listingReply; 
    } 

} 

回答

0

這是我如何解決它

@Override 
public MyObject deserialize(JsonParser jsonParser, DeserializationContext arg1) 
     throws IOException, JsonProcessingException { 

    ObjectCodec oc = jsonParser.getCodec(); 
    JsonNode node = oc.readTree(jsonParser); 

    if (node.getValueAsText() == "") 
     return null; 

    MyObject myObject = new MyObject(); 
    myObject.setMyStirng(node.get("myString").getTextValue()); 

    JsonNode childNode = node.get("childObject"); 
    ObjectMapper objectMapper = new ObjectMapper(); 
    ChildObject childObject = objectMapper.readValue(childNode, 
      ChildObject.class); 

      myObject.setChildObject(childObject); 
      return myObject; 
} 
0

我不知道你需要手動解析響應。你的解決方案會工作,但在我看來似乎不是最佳。由於看起來您正在使用RestTemplate,因此您應該將自己的消息轉換器編寫(或將解析器代碼移至)。然後將此轉換器添加到您的休息模板對象中,該對象將爲您內部反序列化該值。東西沿線,

public class CustomHttpmsgConverter extends AbstractHttpMessageConverter<Object> { 
private ObjectMapper objectMapper = new ObjectMapper(); 
@Override 
protected Object readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { 
    InputStream istream = inputMessage.getBody(); 
    String responseString = IOUtils.toString(istream); 
    if(responseString.isEmpty()) //if your response is empty 
    return null; 
    JavaType javaType = getJavaType(clazz); 
    try { 
     return this.objectMapper.readValue(responseString, javaType); 
    } catch (Exception ex) { 
    throw new HttpMessageNotReadableException(responseString); 
    } 
} 

//add this converter to your resttemplate 
    RestTemplate template = new RestTemplate(); 
    List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(); 
    converters.add(new CustomHttpmsgConverter()); 
    template.setMessageConverters(converters); 
+0

在大多數情況下,我沒有做有關反序列化的東西,這就是壞的數據來自其崩潰解析服務器的特殊情況,因爲解析器期待的對象結構,但響應是一個空字符串(對於結果中的特定字段)。所以這是一個解決方案,僅反序列化我的應用程序中的一個模型。在應用程序的其餘部分,我不必做任何反序列化JSON的工作,它全部由RestTemplate&Jackson自動化(使用@JsonProperty註釋)。 – aryaxt

+0

沒錯,所以我上面的解決方案建議您將此特殊轉換器添加到您的其餘模板中,該模板將處理空響應字符串,從而消除手動解序列化。轉換器提供了精確處理像這些 – nilesh

+0

Ooooh這樣的情況,所以不必處理單個類的反序列化中的錯誤,而是將其移至MessageConverter,然後應用於所有內容。我會試一試,並會回發 – aryaxt