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

Java中POJO字段改为大写后JSON payload未同步变更问题排查

问题描述

我通过POJO生成API请求的JSON时,想把DED、MOP、MPP、LFE、SCR这些字段从小写改成大写,但日志输出的payload里这些字段还是小写,修改没生效。已经试过重建项目、清除缓存、删除旧构建文件和target目录,都没用。相关代码和输出如下:

子POJO代码(AccumCopayDetails)

package com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite;

public class AccumCopayDetails {

   private DED DED;
   private MOP MOP;
   private MPP MPP;
   private LFE LFE;
   private SCR SCR;

   public AccumCopayDetails(com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite.DED DED, com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite.MOP MOP, com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite.MPP MPP, com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite.LFE LFE, com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite.SCR SCR) {
       this.DED = DED;
       this.MOP = MOP;
       this.MPP = MPP;
       this.LFE = LFE;
       this.SCR = SCR;
   }

   @Override
   public String toString() {
       return "AccumCopayDetails{" +
               "DED=" + DED +
               ", MOP=" + MOP +
               ", MPP=" + MPP +
               ", LFE=" + LFE +
               ", SCR=" + SCR +
               '}';
   }

   public com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite.DED getDED() {
       return DED;
   }

   public void setDED(com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite.DED DED) {
       this.DED = DED;
   }
}

父POJO代码(AccumWriteInput)

package com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite;

import com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.CSR.HimClaimPricingDetails;

public class AccumWriteInput {
   private ClaimCore claimCore;
   private PricingSegment pricingSegment;
   private ClaimPricingDetails claimPricingDetails;
   private AccumCopayDetails accumCopayDetails;
   private InsuranceSegment insuranceSegment;
   private String schema;
   private String caseId;
   private String medicalRxIndicator;
   private String networkIndicator;
   private String retailMailOrderIndicator;
   private String brandGenericIndicator;

   @Override
   public String toString() {
       return "AccumWriteInput{" +
               "claimCore=" + claimCore +
               ", pricingSegment=" + pricingSegment +
               ", claimPricingDetails=" + claimPricingDetails +
               ", accumCopayDetails=" + accumCopayDetails +
               ", insuranceSegment=" + insuranceSegment +
               ", schema='" + schema + '\'' +
               ", caseId='" + caseId + '\'' +
               ", medicalRxIndicator='" + medicalRxIndicator + '\'' +
               ", networkIndicator='" + networkIndicator + '\'' +
               ", retailMailOrderIndicator='" + retailMailOrderIndicator + '\'' +
               ", brandGenericIndicator='" + brandGenericIndicator + '\'' +
               '}';
   }
}

POJO调用及请求代码

ClaimCore claimCore = new ClaimCore(1625, 1, 33, 779, 659, "2016-01-10", "000000021284450100");
InsuranceSegment insuranceSegment = new InsuranceSegment(
        "21284450100", "GLORIA", "DELGADO", "P1234567", 1,
        "33", "00", 1, "MO", "Y");

DED DED = new DED("D", 0, 0, 0, 0, " ");
MOP MOP = new MOP("D", 0, 0, 0, 0, " ");
MPP MPP = new MPP("D", 0, 0, 0, 0, " ");
LFE LFE = new LFE("D", 0, 0, 0, 0, " ");
SCR SCR = new SCR("D", 0, 0, 0, 0, " ");

AccumCopayDetails accumCopayDetails = new AccumCopayDetails(DED, MOP, MPP, LFE, SCR);

PricingSegment pricingSegment = new PricingSegment(300.00, 0, 300.00);
com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite.ClaimPricingDetails claimPricingDetails = new com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite.ClaimPricingDetails(
        100.00, 100.00, 0.00,
        200.00, 0.00,
        0.00, 0.00,
        0.00, 0.00,
        0.16, 100.00, "B",
        0.00, 0.00);

AccumWriteInput accumWriteInput = new AccumWriteInput(claimCore, pricingSegment, claimPricingDetails, accumCopayDetails, insuranceSegment,
        " ", "000000000212844501", "P", "I", "R", "G");

S68_1Response = SerenityRest.given().auth().oauth2(DomaniRx_RestAssuredOAuth2.access_token_code)
        .header("platform", "DR2")
        .contentType("application/json")
        .header("Accept", "application/json")
        .log().body()
        .body(accumWriteInput)
        .post(accumWrite)
        .then()
        .statusCode(200)
        .extract().response();

实际日志输出的payload片段

"accumCopayDetails": {
        "mop": {
            "copayUsedType": "D",
            "accumulatorMetCopay": 0,
            "accumulatorMetCoinsurance": 0,
            "accumulatorMetMinimumCopay": 0,
            "accumulatorMetMaximumCopay": 0,
            "accumulatorMetCopayType": " "
        },
        "mpp": {
            "copayUsedType": "D",
            "accumulatorMetCopay": 0,
            "accumulatorMetCoinsurance": 0,
            "accumulatorMetMinimumCopay": 0,
            "accumulatorMetMaximumCopay": 0,
            "accumulatorMetCopayType": " "
        },
        "ded": {
            "copayUsedType": "D",
            "accumulatorMetCopay": 0,
            "accumulatorMetCoinsurance": 0,
            "accumulatorMetMinimumCopay": 0,
            "accumulatorMetMaximumCopay": 0,
            "accumulatorMetCopayType": " "
        },
        "scr": {
            "copayUsedType": "D",
            "accumulatorMetCopay": 0,
            "accumulatorMetCoinsurance": 0,
            "accumulatorMetMinimumCopay": 0,
            "accumulatorMetMaximumCopay": 0,
            "accumulatorMetCopayType": " "
        },
        "lfe": {
            "copayUsedType": "D",
            "accumulatorMetCopay": 0,
            "accumulatorMetCoinsurance": 0,
            "accumulatorMetMinimumCopay": 0,
            "accumulatorMetMaximumCopay": 0,
            "accumulatorMetCopayType": " "
        }
    }

解决方案

问题出在JSON序列化框架(SerenityRest底层用Jackson)的默认行为上:框架会根据getter方法的命名推断JSON字段名,而非直接使用POJO成员变量名。比如getDED()方法,框架会去掉get前缀,将剩余部分首字母小写,最终生成ded字段,和预期的大写不符。

解决办法是给每个需要大写的字段添加@JsonProperty注解,明确指定序列化后的字段名:

修改后的AccumCopayDetails代码

package com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite;

import com.fasterxml.jackson.annotation.JsonProperty;

public class AccumCopayDetails {

   @JsonProperty("DED")
   private DED DED;
   @JsonProperty("MOP")
   private MOP MOP;
   @JsonProperty("MPP")
   private MPP MPP;
   @JsonProperty("LFE")
   private LFE LFE;
   @JsonProperty("SCR")
   private SCR SCR;

   // 构造方法不变
   public AccumCopayDetails(com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite.DED DED, com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite.MOP MOP, com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite.MPP MPP, com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite.LFE LFE, com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite.SCR SCR) {
       this.DED = DED;
       this.MOP = MOP;
       this.MPP = MPP;
       this.LFE = LFE;
       this.SCR = SCR;
   }

   @Override
   public String toString() {
       return "AccumCopayDetails{" +
               "DED=" + DED +
               ", MOP=" + MOP +
               ", MPP=" + MPP +
               ", LFE=" + LFE +
               ", SCR=" + SCR +
               '}';
   }

   @JsonProperty("DED")
   public com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite.DED getDED() {
       return DED;
   }

   @JsonProperty("DED")
   public void setDED(com.domanirx.automation.adjudication.adjcpe.commerc.accum.pojos.AccumCalcWrite.DED DED) {
       this.DED = DED;
   }

   @JsonProperty("MOP")
   public MOP getMOP() {
       return MOP;
   }

   @JsonProperty("MOP")
   public void setMOP(MOP MOP) {
       this.MOP = MOP;
   }

   // MPP、LFE、SCR的getter/setter同理添加对应@JsonProperty注解
}

注意事项:

  • 确保项目引入了Jackson的JsonProperty注解包(com.fasterxml.jackson.annotation.JsonProperty),若未引入,需在Maven/Gradle依赖中添加Jackson相关依赖。
  • 成员变量、对应的getter和setter方法都要添加相同的@JsonProperty("字段名"),避免框架推断冲突。

修改后重新编译运行,JSON输出的字段就会变成大写的DED、MOP等。

内容的提问来源于Stack Exchange,提问作者Muazzam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 06:55:53