
参数校验工具类 
validator参数校验 
- @NotNull:不能为null,但可以为empty(""," ")
 - @NotEmpty:不能为null,而且 
字符串/集合长度必须大于0 (" "," ") - @NotBlank:只能作用在String上,不能为null,而且调用trim()后,长度必须大于0
 
下面的表格整理自validation-api-2.0.0.Final.jar
| Constraint | 详细信息 | 
|---|---|
| @AssertFalse | 该值必须为False | 
| @AssertTrue | 该值必须为True | 
| @DecimalMax(value,inclusive) | 被注释的元素必须是一个数字,其值必须小于等于指定的最大值 ,inclusive表示是否包含该值 | 
| @DecimalMin(value,inclusive) | 被注释的元素必须是一个数字,其值必须大于等于指定的最小值 ,inclusive表示是否包含该值 | 
| @Digits | 限制必须为一个小数,且整数部分的位数不能超过integer,小数部分的位数不能超过fraction | 
| 该值必须为邮箱格式 | |
| @Future | 被注释的元素必须是一个将来的日期 | 
| @FutureOrPresent | 被注释的元素必须是一个现在或将来的日期 | 
| @Max(value) | 被注释的元素必须是一个数字,其值必须小于等于指定的最大值 | 
| @Min(value) | 被注释的元素必须是一个数字,其值必须大于等于指定的最小值 | 
| @Negative | 该值必须小于0 | 
| @NegativeOrZero | 该值必须小于等于0 | 
| @NotBlank | 该值不为空字符串,例如“ ” | 
| @NotEmpty | 该值不为空字符串 | 
| @NotNull | 该值不为Null | 
| @Null | 该值必须为Null | 
| @Past | 被注释的元素必须是一个过去的日期 | 
| @PastOrPresent | 被注释的元素必须是一个过去或现在的日期 | 
| @Pattern(regexp) | 匹配正则 | 
| @Positive | 该值必须大于0 | 
| @PositiveOrZero | 该值必须大于等于0 | 
| @Size(min,max) | 数组大小必须在[min,max]这个区间 | 
另外hibernate的validator包hibernate-validator-6.0.2Final.jar中,又扩展了一些校验
| Constraint | 详细信息 | 
|---|---|
| @CNPJ | CNPJ是巴西联邦税务局秘书处向巴西公司发放的身份证号码,这个注解校验的就是该号码 | 
| @CreditCardNumber(ignoreNonDigitCharacters=) | 被注释的字符串必须通过 Luhn 校验算法,银行卡,信用卡等号码一般都用 Luhn 计算合法性 | 
| @Currency(value=) | 被注释的 javax.money.MonetaryAmount 货币元素是否合规 | 
| @DurationMax(days=, hours=, minutes=, seconds=, millis=, nanos=, inclusive=) | 被注释的元素不能大于指定日期 | 
| @DurationMin(days=, hours=, minutes=, seconds=, millis=, nanos=, inclusive=) | 被注释的元素不能低于指定日期 | 
| @EAN | 被注释的元素是否是一个有效的 EAN 条形码 | 
| @Length(min=, max=) | 被注释的字符串的大小必须在指定的范围内 | 
| @LuhnCheck(startIndex= , endIndex=, checkDigitIndex=, ignoreNonDigitCharacters=) | Luhn 算法校验字符串中指定的部分 | 
| @Mod10Check(multiplier=, weight=, startIndex=, endIndex=, checkDigitIndex=, ignoreNonDigitCharacters=) | Mod10 算法校验 | 
| @Mod11Check(threshold=, startIndex=, endIndex=, checkDigitIndex=, ignoreNonDigitCharacters=, treatCheck10As=, treatCheck11As=) | Mod11 算法校验 | 
| @Range(min=, max=) | 被注释的元素必须在合适的范围内 | 
| @SafeHtml(whitelistType= , additionalTags=, additionalTagsWithAttributes=, baseURI=) | classpath中要有jsoup包,校验是否为安全html,即无注入信息 | 
| @ScriptAssert(lang=, script=, alias=, reportOn=) | 检查脚本是否可运行 | 
| @URL(protocol=, host=, port=, regexp=, flags=) | 被注释的字符串必须是一个有效的url | 
依赖 
xml
<!--jsr 303-->
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.1.0.Final</version>
</dependency>
<!-- hibernate validator-->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.2.0.Final</version>
</dependency>java实现 
实体类
java
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;
import javax.validation.constraints.Pattern;
@Data
public class CustomerRegisterDTO {
    /**
     * 账号
     */
    @Length(max = 10,min = 3,message = "账号必须大于3字符小于10字符")
    @NotBlank(message = "账号不能为空")
    private String account;
    /**
     * 密码
     */
    @NotBlank(message = "密码不能为空")
    private String password;
    /**
     * 邮箱
     */
    @NotBlank(message = "邮箱不能为空")
    @Email(message = "邮箱格式不对")
    private String email;
    @NotBlank(message = "手机号不能为空")
    @Pattern(regexp = "^[1][3,4,5,6,7,8,9][0-9]{9}$", message = "手机号格式有误")
    private String phoneNumber;
    @Range(min = 0,max = 2,message = "用户性别参数有误 用户性别(0男 1女 2未知)")
    @NotEmpty(message = "用户性别不能为空")
    private String sex;
}controller层
java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("api/portal/customer")
public class CustomerControllerAPI{
    @Autowired
    private ICustomerService customerService;
    @ApiOperation(value="注册用户")
    @PostMapping("/register")
    public ResultInfoVO<Void> register(@RequestBody @Validated CustomerRegisterDTO dto){
        customerService.register(dto);
        return ResultInfoVOUtil.buildSuccess();
    }
}返回结果
json
{
    "success": false,
    "code": -1,
    "message": "{\"email\":\"邮箱格式不对\"}",
    "data": null
}
