spring boot 1.4默认使用 hibernate validator
spring boot 1.4默認(rèn)使用 hibernate validator 5.2.4 Final實(shí)現(xiàn)校驗(yàn)功能。hibernate validator 5.2.4 Final是JSR 349 Bean Validation 1.1的具體實(shí)現(xiàn)。
?
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主要使用注解的方式對(duì)bean進(jìn)行校驗(yàn),初步的例子如下所示:
?
然后在controller中可以這樣調(diào)用,加上@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) {} } 如果校驗(yàn)失敗,默認(rèn)會(huì)返回Spring boot 框架的出錯(cuò)信息。是一個(gè)json串,里面有詳細(xì)的出錯(cuò)描述。二 使用gruops 屬性來實(shí)現(xiàn)區(qū)別不同的校驗(yàn)需求
在上面的例子中,如果Student bean想要用于兩個(gè)不同的請(qǐng)求中,每個(gè)請(qǐng)求有不同的校驗(yàn)需求,例如一個(gè)請(qǐng)求只需要校驗(yàn)name字段,一個(gè)請(qǐng)求需要校驗(yàn)name和age兩個(gè)字段,那該怎么做呢?
使用注解的groups屬性可以很好的解決這個(gè)問題,如下所示:
?
根據(jù)需要在@Validated屬性中指定需要校驗(yàn)的分組名,可以指定1到多個(gè)。指定到的分組名會(huì)全部進(jìn)行校驗(yàn),不指定的不校驗(yàn)。
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 注解校驗(yàn)復(fù)雜的業(yè)務(wù)邏輯
如果需要校驗(yàn)的業(yè)務(wù)邏輯比較復(fù)雜,簡(jiǎn)單的@NotBlank,@Min注解已經(jīng)無法滿足需求了,這時(shí)可以使用@ScriptAssert來指定進(jìn)行校驗(yàn)的方法,通過方法來進(jìn)行復(fù)雜業(yè)務(wù)邏輯的校驗(yàn),然后返回 true或false來表明是否校驗(yàn)成功。
例如下面的例子:
?
原文鏈接:http://www.jianshu.com/p/a9b1e2f7a749 著作權(quán)歸作者所有,轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),并標(biāo)注“簡(jiǎn)書作者”。 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
?
總結(jié)
以上是生活随笔為你收集整理的spring boot 1.4默认使用 hibernate validator的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CCF201509-2 日期计算(100
- 下一篇: Jsoup 数据修改