泽西杰克逊序列化父母领域

时间:2012-09-07 04:15:44

标签: java json serialization jersey jackson

我有一个对象“A”,它在对象“A”被序列化时扩展对象“B”,对象“B”中的字段不会被序列化为JSON。有任何想法吗?这是我的对象。

public class CreateNewPlayerAndLoginResponse extends EPNetResponse{
    private PlayerSession playerSession;

    public CreateNewPlayerAndLoginResponse() {
    }

    public CreateNewPlayerAndLoginResponse(Integer playerId, String playerSessionId) {
        this.playerSession = new PlayerSession(playerId, playerSessionId);
    }

    public CreateNewPlayerAndLoginResponse(EPNetErrorEnum error) {
        super(error);
    }

    public CreateNewPlayerAndLoginResponse(EPNetError error) {
        super(error);
    }

    public PlayerSession getPlayerSession() {
        return playerSession;
    }

    public void setPlayerSession(PlayerSession playerSession) {
        this.playerSession = playerSession;
    }

    @Override
    public String toString() {
        return "CreateNewPlayerAndLoginResponse{" + "playerSession=" + playerSession + '}';
    }

}

public class EPNetResponse implements Serializable{

    private EPNetError error;

    public EPNetResponse() {
    }

    public EPNetResponse(EPNetError error) {
        this.error = error;
    }

    public EPNetResponse(EPNetErrorEnum error){
        this.error = new EPNetError(error);
    }

    public EPNetError getError() {
        return error;
    }

    public void setError(EPNetError error) {
        this.error = error;
    }

    @JsonIgnore
    public boolean isError(){
        if(error == null){
            return false;
        }

        return true;
    }
}

public class EPNetError implements Serializable{
    private static final int UNEXPECTED_ERROR = 5417;
    private Integer code;
    private String message;

    public EPNetError() {
    }

    public EPNetError(EPNetErrorEnum error){
        this.code = error.getCode();
        this.message = error.getMessage();
    }

    public EPNetError(EPNetErrorEnum error, String message){
        this.code = error.getCode();
        this.message = message;
    }

    public EPNetError(String message){
        this.code = EPNetError.UNEXPECTED_ERROR;
        this.message = message;
    }

    public EPNetError(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

所有我得到的序列化响应是

{"playerSession":null}

即使我100%确定序列化之前对象的错误消息部分。

另外值得一提的是我使用Jackson 1.9.2和Jersey 1.13

1 个答案:

答案 0 :(得分:1)

如果您希望包含“错误”,则问题是@JsonIgnore中的isError(),而没有任何其他注释。因此,在@JsonProperty旁边添加getError(),它应该包含在内。您可能还需要将其添加到setError()

这里的问题是杰克逊认为@JsonIgnore这里的意思是“忽略属性'错误'”,而你可能只是希望忽略那个特定的访问者。