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 {
//在需要校驗的字段上指定約束條件
 @NotBlankprivate String name;@Min(3)private int age;@NotBlankprivate 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;@NotBlankprivate 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;@NotBlankprivate 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

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542265.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542265.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542265.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python mpi開銷_GitHub - hustpython/MPIK-Means

并行計算的K-Means聚類算法實現一&#xff0c;實驗介紹聚類是擁有相同屬性的對象或記錄的集合&#xff0c;屬于無監督學習&#xff0c;K-Means聚類算法是其中較為簡單的聚類算法之一&#xff0c;具有易理解&#xff0c;運算深度塊的特點.1.1 實驗內容通過本次課程我們將使用C語…

服務器修改開機啟動項,啟動項設置_服務器開機啟動項

最近很多觀眾老爺在苦覓關于啟動項設置的解答&#xff0c;今天欽編為大家綜合5條解答來給大家解開疑惑&#xff01; 有98%玩家認為啟動項設置_服務器開機啟動項值得一讀&#xff01;啟動項設置1.如何在bios設置硬盤為第一啟動項詳細步驟根據BIOS分類的不同操作不同&#xff1a;…

字符串查找字符出現次數_查找字符串作為子序列出現的次數

字符串查找字符出現次數Description: 描述&#xff1a; Its a popular interview question based of dynamic programming which has been already featured in Accolite, Amazon. 這是一個流行的基于動態編程的面試問題&#xff0c;已經在亞馬遜的Accolite中得到了體現。 Pr…

Ubuntu 忘記密碼的處理方法

Ubuntu系統啟動時選擇recovery mode&#xff0c;也就是恢復模式。接著選擇Drop to root shell prompt ,也就是獲取root權限。輸入命令查看用戶名 cat /etc/shadow &#xff0c;$號前面的是用戶名輸入命令&#xff1a;passwd "用戶名" 回車就可以輸入新密碼了轉載于:…

服務器mdl文件轉換,Simulink Project 中 MDL 到 SLX 模型文件格式的轉換

打開彈體示例項目并將 MDL 文件另存為 SLX運行以下命令以創建并打開“sldemo_slproject_airframe”示例的工作副本。Simulink.ModelManagement.Project.projectDemo(airframe, svn);rebuild_s_functions(no_progress_dialog);Creating sandbox for project.Created example fil…

vue 修改div寬度_Vue 組件通信方式及其應用場景總結(1.5W字)

前言相信實際項目中用過vue的同學&#xff0c;一定對vue中父子組件之間的通信并不陌生&#xff0c;vue中采用良好的數據通訊方式&#xff0c;避免組件通信帶來的困擾。今天筆者和大家一起分享vue父子組件之間的通信方式&#xff0c;優缺點&#xff0c;及其實際工作中的應用場景…

Java System類identityHashCode()方法及示例

系統類identityHashCode()方法 (System class identityHashCode() method) identityHashCode() method is available in java.lang package. identityHashCode()方法在java.lang包中可用。 identityHashCode() method is used to return the hashcode of the given object – B…

Linux中SysRq的使用(魔術鍵)

轉&#xff1a;http://www.chinaunix.net/old_jh/4/902287.html 魔術鍵&#xff1a;Linux Magic System Request Key Hacks 當Linux 系統不能正常響應用戶請求時, 可以使用SysRq小工具控制Linux. 一 SysRq的啟用與關閉 要想啟用SysRq, 需要在配置內核時設置Magic SysRq key (CO…

鏈接服務器訪問接口返回了消息沒有活動事務,因為鏈接服務器 SQLEHR 的 OLE DB 訪問接口 SQLNCLI10 無法啟動分布式事務。...

查看一下MSDTC啟動是否正確1、運行 regedt32&#xff0c;瀏覽至 HKEY_LOCAL_MACHINE\Software\Microsoft\MSDTC。添加一個 DWORD 值 TurnOffRpcSecurity&#xff0c;值數據為 1。2、重啟MS DTC服務。3、打開“管理工具”的“組件服務”。a. 瀏覽至"啟動管理工具"。b.…

micropython 蜂鳴器_基于MicroPython的TPYBoard微信遠程可燃氣體報警器的設計與實現...

前言在我們平時的生活中&#xff0c;經常看到因氣體泄漏發生爆炸事故的新聞。房屋起火、人體中毒等此類的新聞報道層出不窮。這種情況下&#xff0c;人民就發明了可燃氣體報警器。當工業環境、日常生活環境(如使用天然氣的廚房)中可燃性氣體發生泄露&#xff0c;可燃氣體報警器…

Java PropertyPermission getActions()方法與示例

PropertyPermission類的getActions()方法 (PropertyPermission Class getActions() method) getActions() method is available in java.util package. getActions()方法在java.util包中可用。 getActions() method is used to get the list of current actions in the form of…

源碼安裝nginx以及平滑升級

源碼安裝nginx以及平滑升級作者&#xff1a;尹正杰版權聲明&#xff1a;原創作品&#xff0c;謝絕轉載&#xff01;否則將追究法律責任。歡迎加入&#xff1a;高級運維工程師之路 598432640這個博客不方便上傳軟件包&#xff0c;我給大家把軟件包放到百度云鏈接&#xff1a;htt…

ajax 跨站返回值,jquery ajax 跨域問題

補充回答&#xff1a;你的動態頁只是一個請求頁。例如你新建一個 get.asp 頁面&#xff0c;用以下代碼&#xff0c;在服務端實現像URL異步(ajax)請求&#xff0c;將請求結果輸出。客戶端頁面再次用ajax(JS或者jquery的)向get.asp請求數據。兩次ajax完成異域數據請求。get.asp代…

Bootstrap學習筆記系列1-------Bootstrap網格系統

目錄 Bootstrap網格系統 學習筆記簡單網格偏移列嵌套列列排序Bootstrap網格系統 學習筆記 簡單網格 先上代碼再解釋 <!DOCTYPE html> <html><head><title>Bootstrap 模板</title><meta charset"utf-8"><!-- 引入 Bootstrap -…

Java類類的getDeclaringClass()方法和示例

類的類getDeclaringClass()方法 (Class class getDeclaringClass() method) getDeclaringClass() method is available in java.lang package. getDeclaringClass()方法在java.lang包中可用。 getDeclaringClass() method is used to return the declared Class object denotin…

樂高泰坦機器人視頻解說_“安防”機器人將亮相服貿會

可巡視園區、自動避障、自動充電&#xff0c;實現24小時巡邏&#xff0c;與后臺鏈接實時視頻監控&#xff0c;異常檢測……17日下午&#xff0c;北青-北京頭條記者在特斯聯科技集團有限公司的展廳中看到&#xff0c;一款“身懷絕技”的“安防”機器人備受關注。這款機器人也將在…

ios上傳文件云服務器上,ios文件上傳服務器

ios文件上傳服務器 內容精選換一換在當前的遷移流程中&#xff0c;可能會存在遷移后ECS控制臺鏡像名稱與實際操作系統不一致的現象。在當前機制下&#xff0c;該現象屬于正常現象。該處顯示的是下發ECS時使用的鏡像名稱&#xff0c;而不是操作系統名稱。如果設置目的端時使用的…

這是一個UIImage集合類,可以很方便的對圖片的染料(著色),增加亮度(閃電)和降低亮度(黑)和其他擴展的功能模塊。...

2019獨角獸企業重金招聘Python工程師標準>>> 這是一個UIImage集合類&#xff0c;可以很方便的對圖片的染料&#xff08;著色&#xff09;&#xff0c;增加亮度&#xff08;閃電&#xff09;和降低亮度&#xff08;黑&#xff09;和其他擴展的功能模塊。 在swift下實…

python爬取酷狗音樂top500_python獲取酷狗音樂top500的下載地址 MP3格式

下面先給大家介紹下python獲取酷狗音樂top500的下載地址 MP3格式&#xff0c;具體代碼如下所示&#xff1a;# -*- coding: utf-8 -*-# Time : 2018/4/16# File : kugou_top500.py# Software: PyCharm# pyVer : python 2.7import requests,jsonheaders{UserAgent : Mozilla/5.0 …

微商相冊一直顯示服務器偷懶,【小程序】微商個人相冊多端小程序源碼以及安裝...

程序介紹學習node.js順便接的400元單子&#xff0c;前后端都是自己寫&#xff0c;相比自己以前寫的&#xff0c;這次相對來說比較規范&#xff0c;用于個人相冊展示&#xff0c;適合微商&#xff0c;有客服聯系&#xff0c;無需后臺管理系統&#xff0c;小程序上直接進行管理&a…