前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java Bean Validation的使用

Java Bean Validation的使用

作者头像
十毛
发布2019-05-14 16:44:04
8120
发布2019-05-14 16:44:04
举报

Spring Web项目中需要校验参数的正确性,JSR 303是其规范,hibernate-validator是该规范的一个实现,使用他可以实现统一的参数校验

添加依赖

因为在Spring Boot中默认已经包含了Hibernate Validator,所以不需要再引入额外的Jar包

配置规则

代码语言:javascript
复制
@Data
public class Person {
    @Null(message = "id should be empty")
    private Integer id;

    @Length(min = 2, max = 10, message = "name的长度为[2-10]之间")
    @NotBlank(message = "name should not be empty")
    private String name;

    @Min(18)
    @Max(130)
    private Integer age;

    @NotNull(message = "gender should not be empty")
    private Boolean gender;

    @Null(message = "createTime should be empty")
    private LocalDateTime createTime;

    @Null(message = "updateTime should be empty")
    private LocalDateTime updateTime;
}

@Min @Max @Length等规则都是在字段不为Null的时候校验,如果字段为Null,则不使用这种规则。如果一定要满足,则还需要添加规则@NotNull

Controller中校验规则

  • 添加注解@Valid
代码语言:javascript
复制
@Slf4j
@SpringBootApplication
@RestController
public class ValidationApplication {
    public static void main(String[] args) {
        SpringApplication.run(ValidationApplication.class, args);
    }

    @GetMapping("ping")
    public String ping() {
        return "success";
    }

    //@Valid注解引导Spring完成参数校验
    @PostMapping("create")
    public WebResult<String> create(@Valid @RequestBody Person person) {
        return new WebResult<>(person.getName());
    }
}

测试

  • 请求的参数
代码语言:javascript
复制
{
    "name": "tenmao"
}
  • 返回的结果
代码语言:javascript
复制
{
    "timestamp": "2019-05-03T10:07:04.611+0000",
    "status": 400,
    "error": "Bad Request",
    "errors": [
        {
            "codes": [
                "NotNull.person.gender",
                "NotNull.gender",
                "NotNull.java.lang.Boolean",
                "NotNull"
            ],
            "arguments": [
                {
                    "codes": [
                        "person.gender",
                        "gender"
                    ],
                    "arguments": null,
                    "defaultMessage": "gender",
                    "code": "gender"
                }
            ],
            "defaultMessage": "gender should not be empty",
            "objectName": "person",
            "field": "gender",
            "rejectedValue": null,
            "bindingFailure": false,
            "code": "NotNull"
        }
    ],
    "message": "Validation failed for object='person'. Error count: 1",
    "path": "/create"
}

校验的结果显示很丰富,但是一般情况下我们并不需要这么详细的校验结果。

自定义返回结果

因为在一般的Restful API接口中,不会把Spring Boot的原始错误信息返回,而是封装成统一的返回格式。 比如

代码语言:javascript
复制
{
    "code":0,
    "msg":"success",
    "data":null
}
  • 自定义返回值:在ValidationApplication的后面添加以下参数校验的异常处理
代码语言:javascript
复制
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public <T> WebResult<T> handleValidationExceptions(
        MethodArgumentNotValidException ex) {
    WebResult<T> result = new WebResult<>();
    result.setCode(400);
    String errorMsg = ex.getBindingResult().getAllErrors().stream()
            .map(DefaultMessageSourceResolvable::getDefaultMessage)
            .collect(Collectors.joining("\n", "[", "]"));
    result.setMsg(errorMsg);
    return result;
}
  • 调整后的结果如下
代码语言:javascript
复制
{
    "code": 400,
    "msg": "[gender should not be empty]",
    "data": null
}

手动校验


在一些非Spring Web项目中,特别是数据收集系统中,也需要对参数进行校验,使用hibernate-validator也会更加便利。

  • 手动校验示例如下:
代码语言:javascript
复制
Person person = new Person();
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Person>> violationSet = validator.validate(person);
for (ConstraintViolation<Person> violation : violationSet) {
    System.out.println(violation.getMessage());
}

规则注解列表


代码语言:javascript
复制
/**
 * 认识一些校验注解Hibernate Validator
 *
 * @NotNull 值不能为空
 * @Null 值必须为空
 * @Pattern(regex= ) 字符串必须匹配正则表达式
 * @Size(min= , max= )集合的元素数量必须在min和max之间
 * @CreditCardNumber(ignoreNoDigitCharacters=) 字符串必须是信用卡的卡号 按照美国的标准
 * @Email 字符串必须是Email地址
 * @Length(mix= , max=) 校验字符串的长度
 * @NotBlank 字符串必须有字符
 * @NotEmpty 字符串不能为Null, 集合有元素
 * @Range(min= , max=) 数字必须大于等于min ,小于等于max
 * @SafeHtml 字符串是安全的html
 * @URL 字符串是合法的URL
 *
 * @AssertFalse 值必须是false
 * @AssertTrue 值必须是true
 *
 * // 此属性既可以是字符串 也可以是数字  inclusive  是包含的意思
 * @DecimalMax(value= ,inclusive=)  值必须小于等于value 指定的值
 * @DecimalMin(value= ,inclusive=)  值必须大于等于value 指定的值
 * @Digits(integer= ,fraction=)
 *
 * @Future  值必须是将来的时间
 * @Past    值必须是过去的时间
 *
 * 此属性必须是数字
 * @Max     值必须小于等于value 指定的值,不能注解在字符串类型的属性上
 * @Min     值必须大于等于value 指定的值,不能注解在字符串类型的属性上
 */

如果这些校验注解不满要求,还可以添加自定义注解,比如@Idcard

参考

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.05.03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 添加依赖
  • 配置规则
  • Controller中校验规则
  • 测试
  • 自定义返回结果
  • 手动校验
  • 规则注解列表
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档