spring boot 1.4默认使用

spring boot 1.4默认使用 hibernate validator_spring

 

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;
}
}