博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate validator验证
阅读量:6006 次
发布时间:2019-06-20

本文共 3496 字,大约阅读时间需要 11 分钟。

1.背景

在我们日常后台的开发中,涉及到很多的字段验证。

public void test(@RequestBody LoginRequest request) {              if (request.getUsername() != null && request.getUsername().length() > 0) {                 。。。            }          }

 

如果request有很多参数,或者是一个表单提交,有很多字段,那我们不是要写很多这样的if判断,可是我们看这样的验证大部分都是相同的,唯独就是字段不一样,那有没有什么便捷的方法呢? 有,就是Hibernate Validateor验证框架,他可以使我们直接使用注解的方式,完成字段验证,加上注解后,剩下的我们只关注自己的逻辑了

 

2.添加依赖

 

org.hibernate
hibernate-validator
5.0.2.Final

 

 

3.常用注解

 

 

 4.当错误的时候,异常提示信息

javax.validation.constraints.AssertFalse.message = must be false      javax.validation.constraints.AssertTrue.message  = must be true      javax.validation.constraints.DecimalMax.message  = must be less than ${inclusive == true ? 'or equal to ' : ''}{value}      javax.validation.constraints.DecimalMin.message  = must be greater than ${inclusive == true ? 'or equal to ' : ''}{value}      javax.validation.constraints.Digits.message      = numeric value out of bounds (<{integer} digits>.<{fraction} digits> expected)      javax.validation.constraints.Future.message      = must be in the future      javax.validation.constraints.Max.message         = must be less than or equal to {value}      javax.validation.constraints.Min.message         = must be greater than or equal to {value}      javax.validation.constraints.NotNull.message     = may not be null      javax.validation.constraints.Null.message        = must be null      javax.validation.constraints.Past.message        = must be in the past      javax.validation.constraints.Pattern.message     = must match "{regexp}"      javax.validation.constraints.Size.message        = size must be between {min} and {max}            org.hibernate.validator.constraints.CreditCardNumber.message = invalid credit card number      org.hibernate.validator.constraints.Email.message            = not a well-formed email address      org.hibernate.validator.constraints.Length.message           = length must be between {min} and {max}      org.hibernate.validator.constraints.NotBlank.message         = may not be empty      org.hibernate.validator.constraints.NotEmpty.message         = may not be empty      org.hibernate.validator.constraints.Range.message            = must be between {min} and {max}      org.hibernate.validator.constraints.SafeHtml.message         = may have unsafe html content      org.hibernate.validator.constraints.ScriptAssert.message     = script expression "{script}" didn't evaluate to true      org.hibernate.validator.constraints.URL.message              = must be a valid URL      org.hibernate.validator.constraints.br.CNPJ.message          = invalid Brazilian corporate taxpayer registry number (CNPJ)      org.hibernate.validator.constraints.br.CPF.message           = invalid Brazilian individual taxpayer registry number (CPF)      org.hibernate.validator.constraints.br.TituloEleitor.message = invalid Brazilian Voter ID card number

 

 

 5.在Controller里面验证使用

@RequestMapping("/register")

public CommonResp register(@Valid @RequestBody RegisterBean register,  BindingResult bingingresult) {

      // 如果验证不通过,错误信息会在BindingResult这个对象中

    if (bingingresult.hasErrors()) {

              return CommonResp(ErrorCode.xxx,  bingingresult.getErrors().get(0).getDefaultMessage());

    }

}

 

 

 

6.也可以自定义错误提示信息

public class UserBean {    @NotEmpty(message="姓名不能为空")    private String name;    @Range(min=20,max=120,message="年龄在20到120岁之间")    private int age;    ...getter/setter...}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载地址:http://alsmx.baihongyu.com/

你可能感兴趣的文章
Kafka High Level Consumer 会丢失消息
查看>>
时间轴
查看>>
入坑vim之配置文件vimrc
查看>>
java 获取系统当前时间的方法
查看>>
Ubuntu 10.04升级git 到1.7.2或更高的可行方法
查看>>
MyBATIS(即iBATIS)问题集
查看>>
Linux下autoconf和automake使用
查看>>
UDP之socket编程
查看>>
Spring Security4实战与原理分析视频课程( 扩展+自定义)
查看>>
Centos6.5升级系统自带gcc4.4.7到gcc4.8.0
查看>>
redis安装与配置文件详解
查看>>
VMware安装失败 “Failed to create the requested registry key Key:installer Error:1021"
查看>>
虚拟化系列-VMware vSphere 5.1 VDP备份管理
查看>>
接口设计
查看>>
同步工具类 java.util.concurrent.CountDownLatch
查看>>
带动量因子的BP网络源码(C#实现)
查看>>
Skia深入分析9——延迟渲染和显示列表
查看>>
mmap函数实现共享内存
查看>>
java笔记
查看>>
贪吃蛇和俄罗斯方块软件
查看>>