maven: 必要jar
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version>
</dependency>
controller:
使用@Validated 注解对参数进行校验,@Validated后面必须是 BindingResult bindingResult 用于接收校验结果
package com.fostlin.jpa.controller;
import com.fostlin.jpa.pojo.UserDO;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
/**
* @Auther: linsiteng
* @Date: 2020/7/29 15:07
* @Description:
*/
@Api(tags = "ObjectVerifyTestController",description="Verify 验证参数注解测试类" )
@RestController
@RequestMapping("/verify")
public class ObjectVerifyTestController {
@ApiOperation(value = "User校验")
@PostMapping("/user")
public String verifyUser(@Validated @RequestBody @ApiParam(value="个人详细信息", required=true)UserDO user, BindingResult bindingResult){
System.out.println(bindingResult.hasErrors());
if(bindingResult.hasErrors()){
return bindingResult.getFieldError().getDefaultMessage();
}
System.out.println(user);
return "校验通过";
}
}
实体类:
注解说明
-
注解说明
1.@NotNull:不能为null,但可以为empty(""," "," ")
2.@NotEmpty:不能为null,而且长度必须大于0 (" "," ")
3.@NotBlank:只能作用在String上,不能为null,而且调用trim()后,长度必须大于0("test") 即:必须有实际字符
复制代码
验证注解 | 验证的数据类型 | 说明 |
---|---|---|
@AssertFalse | Boolean,boolean | 验证注解的元素值是false |
@AssertTrue | Boolean,boolean | 验证注解的元素值是true |
@NotNull | 任意类型 | 验证注解的元素值不是null |
@Null | 任意类型 | 验证注解的元素值是null |
@Min(value=值) | BigDecimal,BigInteger, byte,short, int, long,等任何Number或CharSequence(存储的是数字)子类型 | 验证注解的元素值大于等于@Min指定的value值 |
@Max(value=值) | 和@Min要求一样 | 验证注解的元素值小于等于@Max指定的value值 |
@DecimalMin(value=值) | 和@Min要求一样 | 验证注解的元素值大于等于@ DecimalMin指定的value值 |
@DecimalMax(value=值) | 和@Min要求一样 | 验证注解的元素值小于等于@ DecimalMax指定的value值 |
@Digits(integer=整数位数, fraction=小数位数) | 和@Min要求一样 | 验证注解的元素值的整数位数和小数位数上限 |
@Size(min=下限, max=上限) | 字符串、Collection、Map、数组等 | 验证注解的元素值的在min和max(包含)指定区间之内,如字符长度、集合大小 |
@Past | java.util.Date,java.util.Calendar;Joda Time类库的日期类型 | 验证注解的元素值(日期类型)比当前时间早 |
@Future | 与@Past要求一样 | 验证注解的元素值(日期类型)比当前时间晚 |
@NotBlank | CharSequence子类型 | 验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的首位空格 |
@Length(min=下限, max=上限) | CharSequence子类型 | 验证注解的元素值长度在min和max区间内 |
@NotEmpty | CharSequence子类型、Collection、Map、数组 | 验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0) |
@Range(min=最小值, max=最大值) | BigDecimal,BigInteger,CharSequence, byte, short, int, long等原子类型和包装类型 | 验证注解的元素值在最小值和最大值之间 |
@Email(regexp=正则表达式,flag=标志的模式) | CharSequence子类型(如String) | 验证注解的元素值是Email,也可以通过regexp和flag指定自定义的email格式 |
@Pattern(regexp=正则表达式,flag=标志的模式) | String,任何CharSequence的子类型 | 验证注解的元素值与指定的正则表达式匹配 |
@Valid | 任何非原子类型 | 指定递归验证关联的对象如用户对象中有个地址对象属性,如果想在验证用户对象时一起验证地址对象的话,在地址对象上加@Valid注解即可级联验证 |
package com.fostlin.jpa.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.validator.constraints.Length;
import javax.validation.Valid;
import javax.validation.constraints.*;
import java.io.Serializable;
import java.util.Date;
/**
* @Auther: linsiteng
* @Date: 2020/7/29 15:12
* @Description:
*/
@Data
@ApiModel(description= "个人信息")
/*@Setter
@Getter
@ToString*/
public class UserDO implements Serializable {
@ApiModelProperty(value = "姓名")
@NotBlank(message = "姓名不能为空!")
private String name;
@NotNull(message = "年龄不能为空")
@Min(value = 1,message = "年龄不能小于1")
@ApiModelProperty(value = "年龄")
private Integer age;
@NotNull(message = "性别不能为空")
@Size(min=1,max = 1,message = "性别字段超出长度")
@ApiModelProperty(value = "性别")
private String gender;
/** 邮箱*/
@ApiModelProperty(value = "邮箱")
@NotBlank(message = "联系邮箱不能为空")
@Email(message = "邮箱格式不对")
private String email;
/** 手机号*/
@ApiModelProperty(value = "手机号")
@NotBlank(message = "手机号不能为空")
@Pattern(regexp = "^[1][3,4,5,6,7,8,9][0-9]{9}$", message = "手机号格式有误")
private String mobile;
@ApiModelProperty(value = "时间")
@Past(message = "不能大于当前日期")
private Date date;
@ApiModelProperty(value = "测试字段")
@Valid
private VerifyTest verifyTest;
}