springboot javax.validation 接口参数校验工具

最近在新项目开发中,从前端传来的有很多对象的属性是不能空的,但是如果接收了前端对象再一个一个属性取出来校验,太麻烦,代码很臃肿,于是我搞了一个做对象属性的工具包。在这里简单介绍一下用法

SpringBoot2 项目就不用添加依赖了,web组件已经内置了这个依赖了, Spring 官方也大量的使用了该Jar包。

maven配置

       <dependency>
          <groupId>javax.validation</groupId>
           <artifactId>validation-api</artifactId>
      </dependency>
注解名适用的类型含义
@AssertTrueBoolean, boolean用于boolean字段,该字段只能为true
@AssertFalseBoolean, boolean用于boolean字段,该字段只能为false
@DecimalMax(value=x)BigDecimal, BigInteger, String, byte,short, int, long验证注解的元素值小于等于@ DecimalMax指定的value值
@DecimalMin(value=x)BigDecimal, BigInteger, String, byte,short, int, long验证注解的元素值大于等于@ DecimalMin指定的value值
@Digits(integer=整数位数, fraction=小数位数)
BigDecimal, BigInteger, String, byte,short, int, long
验证注解的元素值的整数位数和小数位数上限
@EmailCharSequence验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式
@Future(integer=整数位数, fraction=小数位数)java.util.Date, java.util.Calendar验证注解的元素值(日期类型)比当前时间晚
@FutureOrPresent(integer=整数位数, fraction=小数位数)java.util.Date, java.util.Calenda验证注解的元素值(日期类型)比当前时间晚或者等于当前时间
@Pastjava.util.Date, java.util.Calendar验证注解的元素值(日期类型)比当前时间早
@PastOrPresentjava.util.Date, java.util.Calendar验证注解的元素值(日期类型)比当前时间早或等于现在
@Max(value=x)BigDecimal, BigInteger, byte, short,int, long验证注解的元素值小于等于@Max指定的value值
@Mix(value=x)BigDecimal, BigInteger, byte, short,int, long验证注解的元素值大于等于@Max指定的value值
@NotBlankCharSequence验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格
@NotEmptyCharSequence验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0)
@NotNullAny type验证注解的元素值不是null
@NullAny type验证注解的元素值是null
@Pattern(regex=正则表达式, flag=)String验证注解的元素值与指定的正则表达式匹配
Size(min=最小值, max=最大值)String, Collection, Map and arrays验证注解的元素值的在min和max(包含)指定区间之内,如字符长度、集合大小

以上是注解的基本说明

只需要在需要校验的对象上加注解就可以了

/**
 * 描述:company表的实体类
 * @version
 * @author:  Administrator
 * @创建时间: 2020-04-16
 */
@Data
@Accessors(chain = true)
@ApiModel
public class Company {
    /**
     * id
     */
    @ApiModelProperty(value = "id")
    private Integer id;

    /**
     * 企业名称
     */
    @NotNull(message = "企业名称是必填字段")
    @NotEmpty(message = "企业名称是必填字段")
    @ApiModelProperty(value = "企业名称")
    private String companyName;

    /**
     * 法人
     */
    @NotNull(message = "法人是必填字段")
    @NotEmpty(message = "法人是必填字段")
    @ApiModelProperty(value = "法人")
    private String corporate;

    /**
     * 地址
     */
    @NotNull(message = "地址是必填字段")
    @NotEmpty(message = "地址是必填字段")
    @ApiModelProperty(value = "地址")
    private String address;

    /**
     * 经度
     */
    @NotNull(message = "经度是必填字段")
    @NotEmpty(message = "经度是必填字段")
    @ApiModelProperty(value = " 经度")
    private String longitude;

    /**
     * 纬度
     */
    @NotNull(message = "纬度是必填字段")
    @NotEmpty(message = "纬度是必填字段")
    @ApiModelProperty(value = " 纬度")
    private String latitude;

    /**
     * 联系人
     */
    @NotNull(message = "联系人是必填字段")
    @NotEmpty(message = "联系人是必填字段")
    @ApiModelProperty(value = " 联系人")
    private String contactPerson;

    /**
     * 电话
     */
    @NotNull(message = "电话是必填字段")
    @NotEmpty(message = "电话是必填字段")
    @ApiModelProperty(value = " 电话")
    private String number;

    /**
     * 企业性质 0建设单位,1施工单位,2监理单位,3检测,4其他
     */
    @NotEmpty(message = "企业性质是必填字段")
    @ApiModelProperty(value = " 0建设单位,1施工单位,2监理单位,3检测,4其他")
    private Integer roleId;
}

在controller接口层注解

加上 @Valid 这个注解就可以了

   /**
     * 新增
     *
     * @param record
     * @return
     */
    @PostMapping("insertSelective")
    @ApiOperation(value = "新增", notes = "")
    public Response insertSelective(@RequestBody @ApiParam(value = "新增") @Valid CompanyInertDTO record) {
        return Response.ok(companyService.insertSelective(record));
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值