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 类有变动,无非正常生成对应的数据库. 解决:使用 ...
随机推荐
- django post分号引发的问题
利用jquery的ajax传值 $.ajax({ type:"POST", url:"", data:"content"=content, ...
- hdu 5755 2016 Multi-University Training Contest 3 Gambler Bo 高斯消元模3同余方程
http://acm.hdu.edu.cn/showproblem.php?pid=5755 题意:一个N*M的矩阵,改变一个格子,本身+2,四周+1.同时mod 3;问操作多少次,矩阵变为全0.输出 ...
- Python流程控制
if语句: --Pyhon中的IF跟其他语言类似,if包含一个逻辑表达式,使用表达式比较,在比较的结果的基础上做出判断 --if expression: statement(s) 注意:Python使 ...
- MySQL主从修复
MySQL主从故障修复 测试库:192.168.1.2 主192.168.1.3 从 192.168.1.4 主 4又是2的从库192.168.1.5 从 有人修改了192.168.1.2和192.1 ...
- iOS的动画效果类型及实现方法
实现iOS漂亮的动画效果主要有两种方法, 一种是UIView层面的, 一种是使用CATransition进行更低层次的控制, 第一种是UIView,UIView方式可能在低层也是使用CATransit ...
- matlab实现不动点迭代、牛顿法、割线法
不动点迭代 function xc = fpi( g, x0, tol ) x(1) = x0; i = 1; while 1 x(i + 1) = g(x(i)); if(abs(x(i+1) - ...
- 如何导入ShareSDK的sample
由于项目需要,最近需要做10几个平台的分享,如果自己去集成,浪费很多时间,而且还很难成功.最后发现Sharesdk,可以满足项目需求. 首先,需要到他们的官网http://sharesdk.cn/下载 ...
- Ubuntu修改屏幕默认亮度
sudo gedit /etc/default/grub 把GRUB_CMDLINE_LINUX="" 改成GRUB_CMDLINE_LINUX="acpi_backli ...
- Careercup - Google面试题 - 5732809947742208
2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...
- ubuntu下修改ip重启系统ip不变
今天同学问我ubuntu下ip如何写死,我想起这周在公司我们队长也问过我,我就在这把我实验的方法说一下. 打开终端: sudo vim /etc/network/interfaces 然后按如下修改: ...