10

我已经看过“杰克逊动态属性名称”这个问题,但它并没有真正回答我的问题。

我想反序列化这样的东西:

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());
}

}

有什么想法可以用注释来清理吗?提前致谢 !

4

2 回答 2

7

Jackson 框架为动态类型提供了内置支持。

//Base type
@JsonTypeInfo(property = "type", use = Id.NAME)
@JsonSubTypes({ @Type(ValidResponse.class), 
                @Type(InvalidResponse.class) 
              })
public abstract class Response<T> {

} 
//Concrete type 1
public class ValidResponse extends Response<T>{

}
//Concrete type 2
public class InvalidResponse extends Response<T>{

}

main {
    ObjectMapper mapper = new ObjectMapper();
    //Now serialize
    ValidResponse response = (ValidResponse)(mapper.readValue(jsonString,      Response.class));

    //Deserialize
    String jsonString = mapper.writeValueAsString(response);
}
于 2014-02-23T13:40:54.083 回答
0

你有没有尝试过:

public class AnyResponse {
    private String status;
    private Error error;
    private Contract contract;
    private User user;
    // And all other possibilities.
}
// ...
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

这应该填充 JSON 中出现的任何对象,并将其余对象保留为空。

然后,您可以使用相关对象填写响应。

于 2013-09-13T13:32:27.703 回答