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

Spring Boot POST请求Controller参数为null的问题排查与解决

问题解决:Spring Boot接收JSON参数为null的问题

你的问题核心原因是实体类属性命名不符合JavaBean规范,导致Spring Boot默认的JSON处理工具Jackson无法正确将请求中的JSON字段映射到实体类属性,仅布尔类型因自带默认值有值,其他字符串属性全部为null,进而触发数据库非空约束报错。以下是两种可行的解决方案:


方案一:修改实体类为标准JavaBean命名规范

JavaBean要求属性名采用小驼峰格式(首字母小写,后续单词首字母大写),对应的getter/setter也需遵循getXxx()/setXxx()(布尔类型可用isXxx())的格式。修改后的SIM_Entity类示例:

public class SIM_Entity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    // 小驼峰命名属性
    private String simCardNo;
    private String mobileNo;
    private boolean status;
    @Temporal(TemporalType.DATE)
    private Date expiryDate;
    private boolean stateOfRegistration;
    private String kyc;
    private String telecomProvider;
    private String fullName;

    // 同步修改构造方法参数名
    public SIM_Entity(String simCardNo, String mobileNo, boolean status, Date expiryDate,
            boolean stateOfRegistration, String kyc, String telecomProvider, String fullName) {
        super();
        this.simCardNo = simCardNo;
        this.mobileNo = mobileNo;
        this.status = status;
        this.expiryDate = expiryDate;
        this.stateOfRegistration = stateOfRegistration;
        this.kyc = kyc;
        this.telecomProvider = telecomProvider;
        this.fullName = fullName;
    }

    public SIM_Entity() {}

    // 规范格式的getter/setter
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public String getSimCardNo() { return simCardNo; }
    public void setSimCardNo(String simCardNo) { this.simCardNo = simCardNo; }

    public String getMobileNo() { return mobileNo; }
    public void setMobileNo(String mobileNo) { this.mobileNo = mobileNo; }

    public boolean isStatus() { return status; }
    public void setStatus(boolean status) { this.status = status; }

    public Date getExpiryDate() { return expiryDate; }
    public void setExpiryDate(Date expiryDate) { this.expiryDate = expiryDate; }

    public boolean isStateOfRegistration() { return stateOfRegistration; }
    public void setStateOfRegistration(boolean stateOfRegistration) { this.stateOfRegistration = stateOfRegistration; }

    public String getKyc() { return kyc; }
    public void setKyc(String kyc) { this.kyc = kyc; }

    public String getTelecomProvider() { return telecomProvider; }
    public void setTelecomProvider(String telecomProvider) { this.telecomProvider = telecomProvider; }

    public String getFullName() { return fullName; }
    public void setFullName(String fullName) { this.fullName = fullName; }

    @Override
    public String toString() {
        return "SIM_Entity [id=" + id + ", simCardNo=" + simCardNo + ", mobileNo=" + mobileNo + ", status="
                + status + ", expiryDate=" + expiryDate + ", stateOfRegistration=" + stateOfRegistration
                + ", kyc=" + kyc + ", telecomProvider=" + telecomProvider + ", fullName=" + fullName + "]";
    }
}

修改后,Postman发送的JSON需对应小驼峰字段名,示例:

{
    "simCardNo": "123456789",
    "mobileNo": "13800138000",
    "status": true,
    "expiryDate": "2025-12-31",
    "stateOfRegistration": true,
    "kyc": "verified",
    "telecomProvider": "China Mobile",
    "fullName": "Zhang San"
}

方案二:使用@JsonProperty注解指定字段映射

若不想修改实体类原有命名,可在每个属性上添加@JsonProperty注解,明确指定对应的JSON字段名,让Jackson能正确识别映射:

public class SIM_Entity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @JsonProperty("sim_card_no")
    private String Sim_card_no;
    
    @JsonProperty("mobile_no")
    private String Mobile_no;
    
    private boolean status;
    
    @JsonProperty("expiry_date")
    @Temporal(TemporalType.DATE)
    private Date Expiry_date;
    
    @JsonProperty("state_of_registration")
    private boolean State_of_registration;
    
    @JsonProperty("kyc")
    private String KYC;
    
    @JsonProperty("telecom_provider")
    private String Telecom_Provider;
    
    @JsonProperty("full_name")
    private String Full_Name;

    // 原有构造方法、getter/setter保持不变
    // ... 原代码的构造方法、getter/setter部分
}

此时Postman发送的JSON字段名需与@JsonProperty指定的一致,示例:

{
    "sim_card_no": "123456789",
    "mobile_no": "13800138000",
    "status": true,
    "expiry_date": "2025-12-31",
    "state_of_registration": true,
    "kyc": "verified",
    "telecom_provider": "China Mobile",
    "full_name": "Zhang San"
}

额外注意事项

  • 确保Postman发送请求时,请求头Content-Type设置为application/json,否则Spring无法识别JSON格式的请求体。
  • 日期字段若需自定义格式,可在属性上添加@JsonFormat(pattern = "yyyy-MM-dd")注解指定解析规则。

内容的提问来源于stack exchange,提问作者Bikash Mohapatra

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 15:54:36