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. EF-Code First 入门

    本文程序基于VS2015.EF6.1,本文不做过多深入讨论,只是个入门. EF 就是微软的 EntityFramework,主要分为 DB First,Model First,Code First.之 ...

  2. AJAX如何接收JSON数据

    简介 在我们了解如何使用AJAX返回JSON数据的时候要先明白下列几点 1. JSON如何来表示对象的 2. JSON如何来表示数组的 var object = { "labId" ...

  3. 暂停更新Blog

    今天非常不好意思的是老魏又要一次的暂停文章跟新了,原因是有些有问题老魏需要从新的梳理,加上这几天工作又开始忙碌起来了,所以这一阵子估计很难有有时间更新了. 不过老魏会抽一下时间更新文章的,不可能像2月 ...

  4. 浅析JVM内存结构和6大区域(转)

    其实对于我们一般理解的计算机内存,它算是CPU与计算机打交道最频繁的区域,所有数据都是先经过硬盘至内存,然后由CPU再从内存中获取数据进行处理,又将数据保存到内存,通过分页或分片技术将内存中的数据再f ...

  5. c语言基础:各种数据类型的输出占位符

    c语言中的输出操作相对java来说是比较麻烦的,每种数据类型的输出都有各自的占位符: 下面是各种数据类型的输出占位符: short/int : %d ; printf("这个整数是:%d&q ...

  6. (转)使用 /proc 文件系统来访问 Linux 内核的内容

    转载网址:http://www.ibm.com/developerworks/cn/linux/l-proc.html 这个虚拟文件系统在内核空间和用户空间之间打开了一个通信窗口/proc 文件系统是 ...

  7. android 讯飞语音识别(离线)注意事项

    讯飞语音识别:使用注意事项:mainfest.xml中一定要记得权限写进去21001:我的情况是没有写SpeechApp类,并且需要在application中注册:20005:无匹配结果23300:本 ...

  8. git如何ignore

    今天新建了一个项目传到git上,但是每次编译都会有一些无用的文件生成,于是就编写了ignore.但是发现无用.因为你的文件已经上传到服务器了,再编写ignore就无用了,ignore的适用是文件没上传 ...

  9. 前端MVC学习——模块发开发、seajs学习

    这份学习链接已经足够了:http://seajs.org/docs/#intro 我假设你至少已经浏览过上述链接文档.并且掌握了基本的seajs基础知识~ 手把手教你创建helloworld~ Hel ...

  10. EBP的妙用[无法使用ESP定律时]

    1.了解EBP寄存器 在寄存器里面有很多寄存器虽然他们的功能和使用没有任何的区别,但是在长期的编程和使用 中,在程序员习惯中已经默认的给每个寄存器赋上了特殊的含义,比如:EAX一般用来做返回值,ECX ...