1

我有一个对象“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

4

1 回答 1

1

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

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

于 2012-09-07T20:33:31.297 回答