@RequestBody入参字段判空校验,后端@Valid参数校验,内部类参数校验

一、@RequestBody配合@Valid使用

    @ApiOperation("上报第三方平台物模型关系")
    @PostMapping
    public ResponseEntity<String> reportModelRel(@RequestBody @Valid PartnerModelRelDto modelRelDto) {
        partnerModelRelService.reportModelRel(modelRelDto);
        return new ResponseEntity<>("Success", HttpStatus.OK);
    }

二、参数接收类中使用注解标注

@NotNull:参数不能为null;

@NotBlank:参数不能为null并且不能为空(比如字符串,不能传个空串"");

@NotEmpty:集合参数不能为null并且不能为空集合;

如果有内部类字段需要判空,则需要在引用内部类的字段上再加一个@Valid注解。

@Data
public class PartnerModelRelDto {

    @Data
    public static class Property extends AuditDomain {

        @ApiModelProperty("第三方平台的功能标识符")
        @NotBlank(message = "sid不能为空")
        private String sid;

        @ApiModelProperty("第三方平台的功能类型")
        private String st;
    }

    @NotNull(message = "id不能为空")
    private Integer id;

    @ApiModelProperty("第三方平台的productId")
    @NotBlank(message = "pidPartner不能为空")
    private String pidPartner;

    @NotEmpty(message = "propertyList不能为空")
    @Valid
    List<Property> propertyList;
}

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值