spring boot 1.4默认使用 hibernate validator
spring boot 1.4默认使用 hibernate validator 5.2.4 Final实现校验功能。hibernate validator 5.2.4 Final是JSR 349 Bean Validation 1.1的具体实现。

How to disable Hibernate validation in a Spring Boot project
As [M. Deinum] mentioned in a comment on my original post, the solution is to set:
spring.jpa.properties.javax.persistence.validation.mode=none
In the application.properties file.
Additionally, this behaviour is described here (its easy to miss because no example is provided).
http://stackoverflow.com/questions/26764532/how-to-disable-hibernate-validation-in-a-spring-boot-project
一 初步使用
hibernate vilidator主要使用注解的方式对bean进行校验,初步的例子如下所示:
package com.query;
import javax.validation.constraints.Min;
import org.hibernate.validator.constraints.NotBlank;
public class Student {
//在需要校验的字段上指定约束条件
@NotBlank
private String name;
@Min(3)
private int age;
@NotBlank
private String classess; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassess() {
return classess;
}
public void setClassess(String classess) {
this.classess = classess;
} }
然后在controller中可以这样调用,加上@Validated注解即可。
package com.controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.learn.validate.domain.Student; @RestController
public class ValidateController { @RequestMapping(value="testStudent")
public void testStudent(@Validated Student student) {
}
}
如果校验失败,默认会返回Spring boot 框架的出错信息。是一个json串,里面有详细的出错描述。
二 使用gruops 属性来实现区别不同的校验需求
在上面的例子中,如果Student bean想要用于两个不同的请求中,每个请求有不同的校验需求,例如一个请求只需要校验name字段,一个请求需要校验name和age两个字段,那该怎么做呢?
使用注解的groups属性可以很好的解决这个问题,如下所示:
package com.query;
import javax.validation.constraints.Min; import org.hibernate.validator.constraints.NotBlank; public class Student {
//使用groups属性来给分组命名,然后在需要的地方指定命令即可
@NotBlank(groups=NAME.class)
private String name;
@Min(value=3,groups=AGE.class)
private int age;
@NotBlank
private String classess; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassess() {
return classess;
}
public void setClassess(String classess) {
this.classess = classess;
} public interface NAME{}; public interface AGE{}; }
根据需要在@Validated属性中指定需要校验的分组名,可以指定1到多个。指定到的分组名会全部进行校验,不指定的不校验。
package com.controller; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.learn.validate.domain.Student;
import com.learn.validate.domain.Student.AGE;
import com.learn.validate.domain.Student.NAME; @RestController
public class ValidateController { @RequestMapping(value="testStudent")
public void testStudent(@Validated Student student) { } @RequestMapping(value="testStudent1")
public void testStudent1(@Validated(NAME.class) Student student) { } @RequestMapping(value="testStudent2")
public void testStudent2(@Validated({NAME.class,AGE.class})
Student student) { }
}
三 使用 @ScriptAssert 注解校验复杂的业务逻辑
如果需要校验的业务逻辑比较复杂,简单的@NotBlank,@Min注解已经无法满足需求了,这时可以使用@ScriptAssert来指定进行校验的方法,通过方法来进行复杂业务逻辑的校验,然后返回 true或false来表明是否校验成功。
例如下面的例子:
package com.query;
import javax.validation.constraints.Min; import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.ScriptAssert; import com.learn.validate.domain.Student.CHECK;
//通过script 属性指定进行校验的方法,传递校验的参数,
//依然可以通过groups属性指定分组名称
@ScriptAssert(lang="javascript",script="com.learn.validate.domain
.Student.checkParams(_this.name,_this.age,_this.classes)",
groups=CHECK.class)
public class Student { @NotBlank(groups=NAME.class)
private String name;
@Min(value=3,groups=AGE.class)
private int age;
@NotBlank
private String classess; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassess() {
return classess;
}
public void setClassess(String classess) {
this.classess = classess;
} public interface NAME{}; public interface AGE{}; public interface CHECK{}; //注意进行校验的方法要写成静态方法,否则会出现
//TypeError: xxx is not a function 的错误
public static boolean checkParams(String name,int age,String classes) {
if(name!=null&&age>8&classes!=null)
{
return true;
}
else
{
return false;
} } }
在需要的地方,通过分组名称进行调用
package com.controller; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.learn.validate.domain.Student;
import com.learn.validate.domain.Student.CHECK; @RestController
public class ValidateController { @RequestMapping(value="testStudent3")
public void testStudent3(@Validated(CHECK.class) Student student) { }
}
import java.util.Set; import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull; public class JavaxValidation {
public static void main(String[] args) {
Dog d = new Dog();
d.setName("小明");
d.setAge(2);
ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
Validator validator = vf.getValidator();
Set<ConstraintViolation<Dog>> set = validator.validate(d);
for (ConstraintViolation<Dog> constraintViolation : set) {
System.out.println(constraintViolation.getMessage());
}
}
} class Dog {
@NotNull(message = "不能为空")
private String name; @Min(value = 1, message = "最少为1")
@Max(value = 20, message = "最大为20")
private int age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}
https://my.oschina.net/p2ng/blog/336690
spring boot 1.4默认使用 hibernate validator的更多相关文章
- 如何在Spring boot中修改默认端口
文章目录 介绍 使用Property文件 在程序中指定 使用命令行参数 值生效的顺序 如何在Spring boot中修改默认端口 介绍 Spring boot为应用程序提供了很多属性的默认值.但是有时 ...
- Spring Boot系列一:默认日志logback配置解析
前言 今天来介绍下Spring Boot如何配置日志logback,我刚学习的时候,是带着下面几个问题来查资料的,你呢 如何引入日志? 日志输出格式以及输出方式如何配置? 代码中如何使用? 正文 Sp ...
- 用spring的@Validated注解和org.hibernate.validator.constraints.*的一些注解在后台完成数据校验
这个demo主要是让spring的@Validated注解和hibernate支持JSR数据校验的一些注解结合起来,完成数据校验.这个demo用的是springboot. 首先domain对象Foo的 ...
- spring boot 系列之二:spring boot 如何修改默认端口号和contextpath
上一篇文件我们通过一个实例进行了spring boot 入门,我们发现tomcat端口号和上下文(context path)都是默认的, 如果我们对于这两个值有特殊需要的话,需要自己制定的时候怎么办呢 ...
- Spring Boot - 修改Tomcat默认的8080端口
前言 默认情况下,Spring Boot内置的Tomcat服务会使用8080端口启动,我们可以使用以下任何技巧去更改默认的Tomcat端口: 注:我们可以通过server.port=0配置,去自动配置 ...
- spring boot中Elasticsearch默认版本问题
这是今天遇上的一个问题. 添加的依赖是7.2.0版本的Elasticsearch,但是其中有两项是6.4.3的,导致我从其他地方移植过来的代码报错. 据大神说,这是因为spring boot中默认的E ...
- Spring Boot去掉浏览器默认的叶子图标
在Spring Boot的配置文件application.properites中添加配置项,可以关闭默认的Favicon spring.mvc.favicon.enabled=false
- Spring Boot 不使用默认的 parent,改用自己的项目的 parent
在初学spring boot时,官方示例中,都是让我们继承一个spring的 spring-boot-starter-parent 这个parent: <parent> <group ...
- Spring Boot启动 Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not instantiate id generator错误
开始运行得很好的项目,因为前一天高度了项目结构和名称突然报上面的错误 查了很多网上资料很多解决方案 造成这个错误的原因有很多,例如 1.@Entity 类有变动,无非正常生成对应的数据库. 解决:使用 ...
随机推荐
- Oracle中的日期
--1.日期字符转换函数的用法 /****************************TO_CHAR********************************/ -------------- ...
- JVM基础:深入学习JVM堆与JVM栈
转自:http://developer.51cto.com/art/201009/227812.htm JVM栈解决程序的运行问题,即程序如何执行,或者说如何处理数据;JVM堆解决的是数据存储的问题, ...
- thinkphp中curl的使用,常用于接口
/lib/action/PublicAction.class.php class PublicAction extends Action{ //curl,返回数组 public function ge ...
- MVC学习系列——ActionResult扩展
首先,MVC扩展性非常强. 我从ActionResult扩展入手,因为我们知道微软ActionResult和其子类,有时候并不能满足所有返回值. 比如:我需要返回XML. 因此,现在我扩展XMLRes ...
- get的四种请求形式
$_GET变量当用户以get方式请求页面并发送数据的时候,$_GET变量就存储了这些数据——get数据!get请求有4种形式:形式1:<form action=”abc.php” meth ...
- Qt Script
旧项目运行在Qt4.x上,要加上一个脚本逻辑,只能上Qt Script.(建议新项目使用QJSEngine) QT += script #include <QtScript> int cp ...
- JAVASCRIPT、ANDROID、C#分别实现普通日期转换多少小时前、多少分钟前、多少秒
貌似最近很流行这个,就写了个js函数实现之 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ...
- c语言编程之循环队列
利用链表实现的循环队列,完成了队列的入队和出队,对于队空和队满用了一个flag进行标记.入队flag++,出队flag-- #include"stdio.h" typedef in ...
- android手机推送视频到服务端
项目需求,android手机向服务器推送视频.苦战几个星期终于实现,现记录下来以免以后忘记. 没做过Java,也没做过Android开发,只能现学现卖.在网上找了下搭建开发a ndroid环境资料, ...
- Fixing:insert_modules not found
搞linux的最怕的就是panic.满屏的报错不知头绪,百度出来的还都是抄来抄去的垃圾. 我遇到的错误已经解决,所以不想再看到报错了..google出来两个没有上下文的文本,因为和他们差不多,在下面贴 ...