为什么在我向POJO添加新字段后,JSON没有改变呢?

huangapple go评论71阅读模式
英文:

Why did not JSON change after I added new fields to POJO

问题

我已经在我的Spring Boot应用程序中的POJO中添加了新的字段,希望在响应中找到它们。这是一个带有Lombok注释的简单POJO:

@Getter
@Setter
@NoArgsConstructor
public class Response implements AsyncResponse, Serializable {
    private String resultCode;
    private String errorCode; // 新添加的字段

    public Response(String resultCode) {
        this.resultCode = resultCode;
    }
}

在我的服务方法中,我创建了一个对象,然后使用setter方法添加了额外的字段errorCode:

Response response = new Response("0");
response.setErrorCode("777");

但是我仍然收到只有一个字段的JSON响应:

{
    "resultCode": "0"
}

我如何强制包含新添加的字段?

更新:
AsyncResponse看起来是这样的:

public interface AsyncResponse {
    String getResultCode();
    void setResultCode(String resultCode);

    String getErrorCode(); // 新添加的getter
    void setErrorCode(String errorCode); // 新添加的setter
}
英文:

I have added new fields to POJO expecting to find them in response in my Spring Boot application. It is a simple POJO with Lombok annotations:

@Getter
@Setter
@NoArgsConstructor
public class Responce implements AsyncResponse, Serializable {
    private String resultCode;
    private String errorCode; // added field

public Responce(String resultCode) {
    this.resultCode = resultCode;
    }
}

In my service method I have created object and then used a setter for added extra field errorCode:

Responce response = new Responce("0");
responce.setErrorCode("777");

But I still receive a JSON with one field:

{
    resultCode: "0"
}

How I can force to include new field?

UPD:
AsyncResponse looks like this

public interface AsyncResponse {
    String getResultCode();
    void setResultCode(String resultCode);

    String getErrorCode(); // added getter
    void setErrorCode(String errorCode); // added setter
}

答案1

得分: 0

在网关模块的application.yml中设置了一个过滤器,其中在fieldsToRetain参数中没有提及字段。

英文:

There was a filter set up in application.yml in gateway module which hadn't mentioned fields in fieldsToRetain parameter.

huangapple
  • 本文由 发表于 2020年9月26日 00:10:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64067731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定