gpt4 book ai didi

java - Jersey Jackson 序列化父字段

转载 作者:太空宇宙 更新时间:2023-11-04 08:04:21 26 4
gpt4 key购买 nike

我有一个对象“A”,它扩展了对象“B”,当对象“A”序列化时,对象“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

最佳答案

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

这里的问题是 Jackson 认为这里的 @JsonIgnore 表示“忽略属性‘错误’”,而您可能只希望忽略该特定访问器。

关于java - Jersey Jackson 序列化父字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12311633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com