0
我收到的數據在JSON自定義反序列化JSON傑克遜排除invalide陣列
{
"status": "INVALID_DATA",
"errors":{ "invalid_id": "Id isn't available",
...
"wrong_address": "Address error msg"
}
}
按鍵及數量結構「錯誤」對我來說是未知的。我試圖用類映射此
@JsonIgnoreProperties(ignoreUnknown = true)
public class StatusErrors
{
private String status;
private HashMap<String, String> errors = new HashMap<String, String>();
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public HashMap<String, String> getErrors() {
return errors;
}
public void setErrors(HashMap<String, String> errors) {
this.errors = errors;
}
}
它工作正常,如果我有「錯誤」,但是當服務器說「OK」,並沒有任何錯誤,它給我
{
"status": "OK",
"errors":[]
}
(唐」請問我是誰寫的服務器) 所以映射器崩潰。 我想寫一個自定義的JsonDeserializer(通用方法)
public abstract class ExcludeEmptyArrayDeserializer<T> extends JsonDeserializer<T> {
private final Class<T> clazz;
protected ExcludeEmptyArrayDeserializer(Class<T> clazz) {
this.clazz = clazz;
}
@Override
public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
ObjectCodec oc = jp.getCodec();
JsonNode node = oc.readTree(jp);
if(node.has("errors")) {
if (node.get("errors").isArray() && !node.get("errors").getElements().hasNext())
((ObjectNode)node).remove("errors");
}
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(node, clazz); // doesn't work
//return oc.treeToValue(node, clazz); // doesn't work too
}
}
public class StatusErrorsDeserializer extends ExcludeEmptyArrayDeserializer<StatusErrors> {
public StatusErrorsDeserializer() {
super(StatusErrors.class);
}
}
結果使用代碼有一個觀點
SimpleModule module = new SimpleModule("", Version.unknownVersion());
module.addDeserializer(StatusErrors.class, new StatusErrorsDeserializer());
ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY).withModule(module);
MappingJacksonHttpMessageConverter messageConverter = new MappingJacksonHttpMessageConverter();
messageConverter.setObjectMapper(mapper);
getRestTemplate().getMessageConverters().clear();
getRestTemplate().getMessageConverters().add(messageConverter);
「錯誤」節點正確刪除,但這個解決方案仍然無法正常工作。 我想我在JsonDeserializer.deserialize方法中犯了一個錯誤,但沒有想法。
BTW StatusErrors類可以是來自服務器的其他複雜消息的基類。
你已經清除了我的視野,男人。謝謝。 – 2014-12-04 17:49:16