Skip to content

Spring 参数校验

pom.xml

pom.xml 中添加 hibernate-validator 的依赖

xml
<!-- hibernate-validator -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.2.4.Final</version>
</dependency>

Demo.java

java
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

public class Demo {
    @NotEmpty(message = "姓名不能为空")
    private String name;

    @NotEmpty(message = "密码不能为空")
    @Length(min = 6, message = "密码长度不能小于 6 位")
    private String password;

    ...
}

DemoController

java
@RequestMapping("/demoAdd")
public String demoAdd(@Valid Demo demo, BindingResult result,  Model model){
    //有错误信息.
    model.addAttribute("demo", demo);
    if (result.hasErrors()) {
        List<ObjectError> list = result.getAllErrors();
        for(ObjectError error:list) {
            System.out.println(error.getCode() + "---" + error.getArguments() + "---" + error.getDefaultMessage());
        }
        return "demo";
    }
    return "/demo";
}

BindingResult所有的错误信息都会保存在这个类中,我们可以使用result.hasErrors()判断是否有错误信息。

校验注解

  • @Null 只能是 null

  • @NotNull 不能为 null 注意用在基本类型上无效,基本类型有默认初始值

  • @AssertFalse 必须为 false

  • @AssertTrue 必须是 true

字符串/数组/集合检查

字符串本身就是个数组

  • @Pattern(regexp="reg") 验证字符串满足正则

  • @Size(max, min) 验证字符串、数组、集合长度范围

  • @NotEmpty 验证字符串不为空或者 null

  • @NotBlank 验证字符串不为 null 或者 trim() 后不为空

数值检查

同时能验证一个字符串是否是满足限制的数字的字符串

  • @Max 规定值得上限 int

  • @Min 规定值得下限

  • @DecimalMax("10.8") 以传入字符串构建一个 BigDecimal,规定值要小于这个值

  • @DecimalMin 可以用来限制浮点数大小

  • @Digits(int1, int2) 限制一个小数,整数精度小于 int1;小数部分精度小于 int2

  • @Digits 无参数,验证字符串是否合法

  • @Range(min=long1,max=long2) 检查数字是否在范围之间

  • 这些都包括边界值

日期检查:Date/Calendar

  • @Past 限定一个日期,日期必须是过去的日期

  • @Future 限定一个日期,日期必须是未来的日期

其他验证

  • @Vaild 递归验证,用于对象、数组和集合,会对对象的元素、数组的元素进行一一校验

  • @Email 用于验证一个字符串是否是一个合法的右键地址,空字符串或 null 算验证通过

  • @URL(protocol=,host=,port=,regexp=,flags=) 用于校验一个字符串是否是合法 URL

示例

java
@Size (min=3, max=20, message="用户名长度只能在 3-20 之间")
@Size (min=6, max=20, message="密码长度只能在 6-20 之间")

@Pattern (regexp="[a-za-z0-9._%+-]+@[a-za-z0-9.-]+\\.[a-za-z]{2,4}", message="邮件格式错误")
@Length(min = 5, max = 20, message = "用户名长度必须位于 5 到 20 之间")
@Email(message = "比如输入正确的邮箱")

@NotNull(message = "用户名称不能为空")
@Max(value = 100, message = "年龄不能大于 100 岁")
@Min(value= 18 ,message= "必须年满 18 岁!" )

@AssertTrue(message = "bln4 must is true")
@AssertFalse(message = "blnf must is falase")

@DecimalMax(value="100",message="decim 最大值是 100")
@DecimalMin(value="100",message="decim 最小值是 100")

@NotNull(message = "身份证不能为空")
@Pattern(regexp="^(\\d{18,18}|\\d{15,15}|(\\d{17,17}[x|X]))$", message="身份证格式错误")

参考

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.