【问题标题】:Why did not JSON change after I added new fields to POJO为什么我在 POJO 中添加新字段后 JSON 没有变化
【发布时间】:2021-01-12 00:03:56
【问题描述】:

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

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

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

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

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

但我仍然收到一个包含一个字段的 JSON:

{
    resultCode: "0"
}

如何强制包含新字段?

更新: AsyncResponse 看起来像这样

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

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

【问题讨论】:

  • 你能展示这个AsyncResponse类吗?或ResponceAsyncResponse 上使用的所有杰克逊注解
  • 您是否尝试过清理并重新构建。 ?或尝试除errorCode之外的其他方法
  • 你用的是spring boot的嵌入式tomcat?能否验证一下编译时生成的war包是否更新了?
  • @Deadpool 在 POJO 中除了提到的类级别上没有注释。 AsyncResponce 中也没有注释。
  • @abs_dev 它是一个 tomcat,真的。我们在交付管道中使用 Jenkins,所以据我所知,是的 jar 已更新。

标签: java json spring-boot jackson lombok


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多