我已经看过“杰克逊动态属性名称”这个问题,但它并没有真正回答我的问题。
我想反序列化这样的东西:
public class Response<T> {
private String status;
private Error error;
private T data;
}
但是数据可以有不同的名称,因为存在不同的服务并返回具有一些不同数据的相同结构。例如“用户”和“合同”:
{
response: {
status: "success",
user: {
...
}
}
}
或者
{
response: {
status: "failure",
error : {
code : 212,
message : "Unable to retrieve contract"
}
contract: {
...
}
}
}
我想像这样通用化我的响应对象:
public class UserResponse extends Response<User> {}
我尝试了以下方法,但我不确定这是我的用例,或者是否没有以好的方式使用它:
@JsonTypeInfo(include = As.WRAPPER_OBJECT, use = Id.CLASS)
@JsonSubTypes({@Type(value = User.class, name = "user"),
@Type(value = Contract.class, name = "contract")})
最后,我创建了一个自定义反序列化器。它有效,但我不满意:
public class ResponseDeserializer extends JsonDeserializer<Response> {
@Override
public Response deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
ObjectMapper mapper = new ObjectMapper();
Response responseData = new Response();
Object data = null;
for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
String propName = jp.getCurrentName();
// Skip field name:
jp.nextToken();
if ("contract".equals(propName)) {
data = mapper.readValue(jp, Contract.class);
} else if ("user".equals(propName)) {
data = mapper.readValue(jp, User.class);
} else if ("status".equals(propName)) {
responseData.setStatus(jp.getText());
} else if ("error".equals(propName)) {
responseData.setError(mapper.readValue(jp, com.ingdirect.dg.business.object.community.api.common.Error.class));
}
}
if (data instanceof Contract) {
Response<Contract> response = new Response<Ranking>(responseData);
return response;
}
if (data instanceof User) {
Response<User> response = new Response<User>(responseData);
return response;
}
// in all other cases, the type is not yet managed, add it when needed
throw new JsonParseException("Cannot parse this Response", jp.getCurrentLocation());
}
}
有什么想法可以用注释来清理吗?提前致谢 !