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) { }
}
原文链接:http://www.jianshu.com/p/a9b1e2f7a749
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
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的更多相关文章

  1. 如何在Spring boot中修改默认端口

    文章目录 介绍 使用Property文件 在程序中指定 使用命令行参数 值生效的顺序 如何在Spring boot中修改默认端口 介绍 Spring boot为应用程序提供了很多属性的默认值.但是有时 ...

  2. Spring Boot系列一:默认日志logback配置解析

    前言 今天来介绍下Spring Boot如何配置日志logback,我刚学习的时候,是带着下面几个问题来查资料的,你呢 如何引入日志? 日志输出格式以及输出方式如何配置? 代码中如何使用? 正文 Sp ...

  3. 用spring的@Validated注解和org.hibernate.validator.constraints.*的一些注解在后台完成数据校验

    这个demo主要是让spring的@Validated注解和hibernate支持JSR数据校验的一些注解结合起来,完成数据校验.这个demo用的是springboot. 首先domain对象Foo的 ...

  4. spring boot 系列之二:spring boot 如何修改默认端口号和contextpath

    上一篇文件我们通过一个实例进行了spring boot 入门,我们发现tomcat端口号和上下文(context path)都是默认的, 如果我们对于这两个值有特殊需要的话,需要自己制定的时候怎么办呢 ...

  5. Spring Boot - 修改Tomcat默认的8080端口

    前言 默认情况下,Spring Boot内置的Tomcat服务会使用8080端口启动,我们可以使用以下任何技巧去更改默认的Tomcat端口: 注:我们可以通过server.port=0配置,去自动配置 ...

  6. spring boot中Elasticsearch默认版本问题

    这是今天遇上的一个问题. 添加的依赖是7.2.0版本的Elasticsearch,但是其中有两项是6.4.3的,导致我从其他地方移植过来的代码报错. 据大神说,这是因为spring boot中默认的E ...

  7. Spring Boot去掉浏览器默认的叶子图标

    在Spring Boot的配置文件application.properites中添加配置项,可以关闭默认的Favicon spring.mvc.favicon.enabled=false

  8. Spring Boot 不使用默认的 parent,改用自己的项目的 parent

    在初学spring boot时,官方示例中,都是让我们继承一个spring的 spring-boot-starter-parent 这个parent: <parent> <group ...

  9. Spring Boot启动 Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not instantiate id generator错误

    开始运行得很好的项目,因为前一天高度了项目结构和名称突然报上面的错误 查了很多网上资料很多解决方案 造成这个错误的原因有很多,例如 1.@Entity 类有变动,无非正常生成对应的数据库. 解决:使用 ...

随机推荐

  1. 机器学习实战——k-邻近算法:约会网站

    1.kNN 算法 算法说明: set<X1,X2……Xn> 为已知类别数据集,预测 点Xt 的类别: (1)计算中的set中每一个点与Xt的距离 (2)按距离增序排列 (3)选择距离最小的 ...

  2. Extjs 4.2 grid 分页问题,点击下一页参数没带过去

    最初的store写法: var store = Ext.create('Ext.data.Store', { model: 'PKU',//这个地方CarPKU不是一个对象,而是一个类 remoteS ...

  3. vs2010旗舰版后,运行调试一个项目时调试不了,提示的是:无法使用“pc”附加到应用程序“webdev.webserver40.exe(PID:2260”

    具体问题描述: vs2010旗舰版后,运行调试一个项目时调试不了,能编译,按ctrl+f5 可以运行,但是就是调试就不行,提示的是:无法使用“pc”附加到应用程序“webdev.webserver40 ...

  4. C++实现数字媒体二维图像变换

    C++实现数字媒体二维图像变换 必备环境 glut.h 头文件 glut32.lib 对象文件库 glut32.dll 动态连接库 程序说明 C++实现了用glut画正方形,画三角形的功能.并附带放大 ...

  5. 顺序容器:vector,deque,list

    1.顺序容器:vector,deque,list 容器类共享公共接口,只要学会其中一种类型就能运用另一种类型.每种容器提供一组不同的时间和功能这种方案,通常不需要修改代码,秩序改变类型声明,每一种容器 ...

  6. LCA专题

    标签(空格分隔): LCA 我的个人网站挂了,最近就先用这个来写博客吧.以后争取在这个网站写一些与OI无关的个人爱好的东西. 题目来源:code[VS] 倍增--在线算法 用 $f[i][j]$ 记录 ...

  7. LintCode-Compare Strings

    Compare two strings A and B, determine whether A contains all of the characters in B. The characters ...

  8. Ubuntu多系统安装注意事项

    1. 安装 选择分区时一定要全设置成逻辑分区,不能是主分区! 2.多系统引导向修复 利用LiveCD制作U盘启动进入Ubuntu系统,若挂载点为: /dev/sda9             swap ...

  9. [原创] zabbix学习之旅四:mail客户端安装

    相信大家使用zabbix的最主要目的就是当被监控机器发生故障时,能通过zabbix获得第一时间的报警提醒.zabbix常用的报警媒介有email,短信,jabber和脚本,这其中脚本类型最为灵活,尤其 ...

  10. oracle 11g行转列 列转行

    行转列: SELECT *   FROM   src_table   UNPIVOT (param_value FOR param_name IN (product_color AS 'product ...