springboot下对于接口入参是对象且使用了@RequestBody注解,但是入参对象的字段是大写字母开头的,请求时如果不处理,会转为小写或者驼峰式

接口格式如下:

@PostMapping("/MedicalInformation")
    @ApiOperation(value = "XXX接口描述")
    @ResponseBody
    public ResultResponse MedicalInformation(@RequestBody ProcessDto processDto) {
        try {
            MedicalInformationDto medicalInformationDto = processDto.getRequestContent();
            if(medicalInformationDto == null){
                return ResultResponse.fail(null, ErrorCode.CORE_IS_NULL.getMessage(),ErrorCode.CORE_IS_NULL.getCode());
            }
            JSONObject jsonObject = hsCasesService.saveProcessData(medicalInformationDto);
            return ResultResponse.ok(jsonObject,"操作成功","00000");
        }catch (Exception e){
            e.printStackTrace();
            return ResultResponse.fail(null,"系统异常,请联系管理员!","00001");
        }
    }

接口入参对象如下:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProcessDto {
    private String AUTHCode;
    private String Version;
    private String RequestTime;
    private MedicalInformationDto RequestContent;
}

请求如下:

在这里插入图片描述
接口收到请求如下:
在这里插入图片描述
发现接口请求的入参与接口定义的入参的字段完全一样,但是却没有收到参数值???

原因:

当请求接口的入参是对象时会使用@RequestBody注解将请求的入参(json格式)映射到相应的对象属性上,这时候就会将入参的字段与接口定义的字段进行匹配,虽然接口定义中声明的是大写的,但是底层默认将其转换为驼峰格式的字段名,如下:在这里插入图片描述

这种情况下可以使用@JsonProperty注解作用在字段或方法上,用来对属性在序列化/反序列化时,可以用它来避免遗漏属性,同时提供对属性名称重命名,如下:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProcessDto {
    @JsonProperty(value = "AUTHCode")
    private String AUTHCode;
    @JsonProperty(value = "Version")
    private String Version;
    @JsonProperty(value = "RequestTime")
    private String RequestTime;
    @JsonProperty(value = "RequestContent")
    private MedicalInformationDto RequestContent;
}

问题解决

修改后同样使用上述请求触发,如下:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值