You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot修改实体与数据库列名后接口返回未同步怎么解决?

问题根因

你遇到的是Jackson序列化规则的问题,和JPA数据库映射配置无关,因此修改spring.jpa.hibernate.ddl-auto不会生效:
实体类中outcomeSellPrice属性对应的Getter/Setter方法名仍为旧命名getSoldAt/setSoldAt,Spring Boot默认使用Jackson做JSON序列化时,会根据Getter方法名生成返回字段,方法名getSoldAt会被解析为字段名soldAt,因此响应中一直显示旧字段名。

解决方案

你可以根据需求选择以下任意一种方案:

方案1:修正Getter/Setter命名(推荐)

将当前代码中outcomeSellPrice对应的Getter/Setter方法改为和属性名匹配的命名:

// 替换原来的getSoldAt方法
public BigDecimal getOutcomeSellPrice() {
    return outcomeSellPrice;
}

// 替换原来的setSoldAt方法
public void setOutcomeSellPrice(BigDecimal outcomeSellPrice) {
    this.outcomeSellPrice = outcomeSellPrice;
}

修改后默认序列化返回的字段会是outcomeSellPrice(小驼峰格式)。

方案2:强制指定JSON序列化字段名

如果你希望返回的字段名和数据库列名完全一致为outcome_sell_price(下划线格式),可以直接在属性上添加@JsonProperty注解指定序列化名称:

@Column(name="outcome_sell_price")
@JsonProperty("outcome_sell_price")
private BigDecimal outcomeSellPrice;

该方案不需要修改Getter/Setter命名,配置后会直接按照你指定的名称返回字段。

验证步骤

修改完成后重新编译、启动Spring Boot项目,再次调用接口即可看到返回字段已同步更新。

内容的提问来源于stack exchange,提问作者Micah Ludwick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 20:24:02