我以JSON
接收数据{
"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类可以是来自服务器的其他复杂消息的基类。
答案 0 :(得分:1)
最简单的解决方案是将错误变量声明更改为Object:
@JsonIgnoreProperties(ignoreUnknown = true)
class StatusErrors {
private String status;
private Object errors;
public Map<String, String> getErrorsMap() {
if (this.errors instanceof Map) {
return (Map)this.errors;
}
return null;
}
....
您不需要任何序列化程序和反序列化程序:
ObjectMapper objectMapper = new ObjectMapper();
StatusErrors result1 = objectMapper.readValue(JSON1, StatusErrors.class);
System.out.println(result1);
System.out.println(result1.getErrors().getClass());
System.out.println(result1.getErrorsMap());
StatusErrors result2 = objectMapper.readValue(JSON2, StatusErrors.class);
System.out.println(result2);
上面的代码将打印:
StatusErrors(status=INVALID_DATA, errors={invalid_id=Id isn't available, wrong_address=Address error msg})
class java.util.LinkedHashMap
{invalid_id=Id isn't available, wrong_address=Address error msg}
StatusErrors(status=OK, errors=[])