亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot后端進(jìn)行數(shù)據(jù)校驗(yàn)JSR303的使用詳解

瀏覽:47日期:2023-03-22 11:51:24

如果只想查看注解,請?zhí)轿恼履┪膊糠?/b>

簡介

在前后端進(jìn)行數(shù)據(jù)交互中,在前端把數(shù)據(jù)傳送到后端前,一般會先進(jìn)行校驗(yàn)一次,校驗(yàn)成功之后,才把數(shù)據(jù)發(fā)送到后端。但是我們在服務(wù)端還得在對數(shù)據(jù)進(jìn)行一次校驗(yàn)。因?yàn)檎埱髷?shù)據(jù)發(fā)送的鏈接很容易獲取,可以不經(jīng)過前端界面,使用postman等工具直接向后臺發(fā)送數(shù)據(jù),這就可能造成發(fā)送的數(shù)據(jù)是不合法的情況。

項(xiàng)目創(chuàng)建

首先創(chuàng)建一個springboot項(xiàng)目

使用的springboot版本為:(本文代碼以該版本為準(zhǔn),不同版本springboot,在下面內(nèi)容會出現(xiàn)一些差異

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>

引入如下依賴

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> </dependencies>

這個作標(biāo)在新一點(diǎn)的springboot版本中,需要單獨(dú)引入。在老版本是默認(rèn)引入的。這個是用來引入對jsr303注解的支持。

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>

接著創(chuàng)建一個Java Bean

package cn.jxj4869.demo.entity;import lombok.Data;import javax.validation.constraints.NotNull;@Datapublic class User { @NotNull private Integer id; private String username; private String password; private String email;}

返回類型的JavaBean

package cn.jxj4869.demo.entity;import java.util.HashMap;public class R extends HashMap<String, Object> {private static final long serialVersionUID = 1L;public R() {put('code', 0);put('msg', 'success');}public static R error(int code, String msg) {R r = new R();r.put('code', code);r.put('msg', msg);return r;}public static R ok(String msg) {R r = new R();r.put('msg', msg);return r;}public R put(String key, Object value) {super.put(key, value);return this;}}

創(chuàng)建一個controller。

index方法用來跳轉(zhuǎn)到首頁。

package cn.jxj4869.demo.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;@Controllerpublic class UserController { @RequestMapping('/') public String index(){ return 'index'; }}

首頁代碼放到resources/templates目錄下

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title> <style> div{ margin-top: 50px; } </style></head><body><div> 新增表單 <br><br> <form method='post'> <label>用戶名</label> <input type='text' name='username'/> <br> <label>密碼</label> <input type='text' name='password'/> <br> <label>郵箱</label> <input type='email' name='email'/> <br> <input type='submit' value='提交'/> </form></div><div> 更新表單 <br><br> <form method='post'> <input type='hidden' name='id' value='1'> <label>用戶名</label> <input type='text' name='username'/> <br> <label>密碼</label> <input type='text' name='password'/> <br> <label>郵箱</label> <input type='email' name='email'/> <br> <input type='submit' value='提交'/> </form></div></body></html>傳統(tǒng)的檢驗(yàn)方式

要在后端進(jìn)行數(shù)據(jù)校驗(yàn),傳統(tǒng)的校驗(yàn)方式在controller層接受數(shù)據(jù)后,按照要求對數(shù)據(jù)進(jìn)行校驗(yàn)

比如要接收一個user bean對象。

現(xiàn)在要對user對象中的username屬性進(jìn)行非空校驗(yàn),password屬性進(jìn)行非空校驗(yàn)和長度校驗(yàn)。

@PostMapping('/user') @ResponseBody public R user1(User user) throws Exception { if(StringUtils.isEmpty(user.getUsername())) { return R.error(400,'username不能為空'); } if(StringUtils.isEmpty(user.getPassword())||user.getPassword().length()>8||user.getPassword().length() <4) { return R.error(400,'password無效'); } return null; }

如果有多個方法都需要接受user對象, 而且要校驗(yàn)的屬性可能不止username和password這兩個屬性,如果每個方法里面都采用上面這種校驗(yàn)方式的話,代碼就會很臃腫,而且不好維護(hù),當(dāng)改變了userbean的屬性,或者對校驗(yàn)規(guī)則進(jìn)行修改后,就得對所有的校驗(yàn)代碼進(jìn)行更新。 這是一件工程量很大的事。

使用JSR303

為了解決上述問題,我們可以使用JSR303提供的注解進(jìn)行校驗(yàn)。

JSR是Java Specification Requests的縮寫,意思是Java 規(guī)范提案。JSR303也就是第303號提案。

使用JSR303的方法很簡單,例如上面的需求,我們只需要在user的屬性上加上注解即可。

步驟如下:

1、給Bean添加校驗(yàn)注解,一般是在 javax.validation.constraints這個包下,也還有一些是hibernate提供的。

2、開啟校驗(yàn)功能@Valid。

3、當(dāng)校驗(yàn)失敗的時候,會拋出org.springframework.validation.BindException異常。

常用的校驗(yàn)注解在文末

package cn.jxj4869.demo.entity;import lombok.Data;import org.hibernate.validator.constraints.Length;import javax.validation.constraints.NotNull;@Datapublic class User { private Integer id; @NotBlank private String username; @NotBlank @Length(min = 4,max = 8) private String password; private String email;}

然后在controller里面的方法上,加上@Valid注解即可

@PostMapping('/user2') @ResponseBody public R user2(@Valid User user) throws Exception { System.out.println(user); return null; }

當(dāng)校驗(yàn)失敗后,會出現(xiàn)如下錯誤。并且會給出默認(rèn)的提示信息。

SpringBoot后端進(jìn)行數(shù)據(jù)校驗(yàn)JSR303的使用詳解

自定義錯誤信息

那這個錯誤信息是怎么來的呢。

進(jìn)入@NotNULL注解的代碼里面

@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })@Retention(RUNTIME)@Repeatable(List.class)@Documented@Constraint(validatedBy = { })public @interface NotBlank {String message() default '{javax.validation.constraints.NotNull.message}';............ ............ ............}

會有一個message屬性。顯然就是指定錯誤的提示內(nèi)容。而這些錯誤提示是在一個叫validationMessages.properties的文件中,用idea的搜索工具可以找到,雙擊shift鍵打開搜索。

發(fā)現(xiàn)有這么多validationMessages.properties的文件,而且支持國際化。

SpringBoot后端進(jìn)行數(shù)據(jù)校驗(yàn)JSR303的使用詳解

打開validationMessages_zh.properties,可以看到里面定義了這么多的提示。而錯誤提示就是從這文件中獲取的。

SpringBoot后端進(jìn)行數(shù)據(jù)校驗(yàn)JSR303的使用詳解

如果我們不想用默認(rèn)的校驗(yàn)提示信息的話,可以自己指定。

指定message的值即可。

@NotBlank(message = '用戶名不能為空') private String username;

SpringBoot后端進(jìn)行數(shù)據(jù)校驗(yàn)JSR303的使用詳解

錯誤信息的獲取與響應(yīng)

當(dāng)校驗(yàn)出錯時,會默認(rèn)返回一個錯誤界面,或者返回錯誤提示的json數(shù)據(jù)。但默認(rèn)提供的顯然不是我們想要的,如果可以拿到錯誤信息,那我們就能自定義相應(yīng)數(shù)據(jù)了。

拿到錯誤信息的方式也很簡單,只要在方法中加上BindingResult result這個參數(shù),錯誤信息就會封裝這里面。

@PostMapping('/user2') @ResponseBody public R user2(@Valid User user, BindingResult result) throws Exception { System.out.println(user); if(result.hasErrors()) { //判斷是否有錯誤 Map<String,String> map = new HashMap<>(); //1、獲取校驗(yàn)的錯誤結(jié)果 result.getFieldErrors().forEach((item)->{ //FieldError 獲取到錯誤提示 String message = item.getDefaultMessage(); //獲取錯誤的屬性的名字 String field = item.getField(); map.put(field,message); }); return R.error(400,'提交的數(shù)據(jù)不合法').put('data',map); } // 若沒有錯誤,則進(jìn)行接下去的業(yè)務(wù)操作。 return null; }

不過不推薦上面這種方式,理由同上,當(dāng)校驗(yàn)的地方多了,每個方法里面都加上這么個異常處理,會讓代碼很臃腫。

不知道你們是否還記得,springmvc里面有個全局的異常處理,我們可以自定義一個異常處理,在這里面統(tǒng)一處理異常。

統(tǒng)一處理BinException。這樣就可以不用在controller中去處理錯誤信息了。

package cn.jxj4869.demo.execption;import cn.jxj4869.demo.entity.R;import org.springframework.validation.BindException;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.RestControllerAdvice;import java.util.HashMap;import java.util.Map;@RestControllerAdvice(basePackages = 'cn.jxj4869.demo.controller')public class MyExceptionControllerAdvice { @ExceptionHandler(value = BindException.class) public R handleVaildException(BindException e) { Map<String,String> map = new HashMap<>(); //1、獲取校驗(yàn)的錯誤結(jié)果 e.getFieldErrors().forEach((item)->{ //FieldError 獲取到錯誤提示 String message = item.getDefaultMessage(); //獲取錯誤的屬性的名字 String field = item.getField(); map.put(field,message); }); return R.error(400,'提交的數(shù)據(jù)不合法').put('data',map); }}錯誤異常類型補(bǔ)充

校驗(yàn)出錯的時候,會拋出兩種異常

org.springframework.validation.BindException

使用@Valid注解進(jìn)行校驗(yàn)的時候拋出的

org.springframework.web.bind.MethodArgumentNotValidException

使用@validated校驗(yàn)的時候拋出的

在異常捕獲中加入下面這個

@ExceptionHandler(value= MethodArgumentNotValidException.class) public R handleVaildException(MethodArgumentNotValidException e){ BindingResult bindingResult = e.getBindingResult(); Map<String,String> map = new HashMap<>(); bindingResult.getFieldErrors().forEach((fieldError)->{ map.put(fieldError.getField(),fieldError.getDefaultMessage()); }); return R.error(400,'提交的數(shù)據(jù)不合法').put('data',map); }分組校驗(yàn)

在不同業(yè)務(wù)場景下,校驗(yàn)規(guī)則是不一樣的,比如user對象中id這個屬性,在新增的時候,這個屬性是不用填的,要為null,但是在修改的時候,id屬性是不能為null的。

可以用注解中的groups屬性來指定,在什么場合下使用改注解

@Documented@Constraint(validatedBy = { })@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })@Retention(RUNTIME)@Repeatable(List.class)public @interface NotBlank {Class<?>[] groups() default { };........}

首先定義兩個接口AddGroup和UpdateGroup,不需要做任何實(shí)現(xiàn)

package cn.jxj4869.demo.valid;public interface UpdateGroup {}

package cn.jxj4869.demo.valid;public interface AddGroup {}

在user中指定group。

id屬性在AddGroup的時候,要為null,在UpdateGroup的時候不能為null username屬性在AddGroup和Update的時候,都要進(jìn)行校驗(yàn),不能為空。 password屬性,當(dāng)校驗(yàn)的時候指定分組的話,會不起作用,因?yàn)闆]有給它指定校驗(yàn)的分組

package cn.jxj4869.demo.entity;import cn.jxj4869.demo.valid.AddGroup;import cn.jxj4869.demo.valid.UpdateGroup;import lombok.Data;import javax.validation.constraints.NotBlank;import javax.validation.constraints.NotEmpty;import javax.validation.constraints.NotNull;import javax.validation.constraints.Null;@Datapublic class User { @Null(groups = {AddGroup.class}) @NotNull(groups = {UpdateGroup.class}) private Integer id; @NotBlank(message = '用戶名不能為空',groups = {AddGroup.class,UpdateGroup.class}) private String username; @NotEmpty private String password; private String email;}

在controller中用@Validated注解,指定校驗(yàn)的分組

@PostMapping('/user3') @ResponseBody public R user3(@Validated(UpdateGroup.class) User user) { System.out.println(user); return null; }

結(jié)果如下圖所示,因?yàn)閜assword屬性沒有指定校驗(yàn)的分組,所以在校驗(yàn)的時候,都不會對它進(jìn)行合法性檢查。

SpringBoot后端進(jìn)行數(shù)據(jù)校驗(yàn)JSR303的使用詳解

SpringBoot后端進(jìn)行數(shù)據(jù)校驗(yàn)JSR303的使用詳解

自定義校驗(yàn)

當(dāng)提供的注解不能滿足我們需求的時候,可以自定義注解。

例如我們現(xiàn)在給user新加一個屬性status,并要求這個屬性的值只能是0或者1。

新建一個@StatusValue注解。

根據(jù)jsr303的規(guī)范,校驗(yàn)注解得有三個屬性。

message:用來獲取錯誤提示的 groups:指定校驗(yàn)分組的。 payload:可以自定義一些負(fù)載信息

使用@Constraint注解指定該注解的校驗(yàn)器

package cn.jxj4869.demo.valid;import javax.validation.Constraint;import javax.validation.Payload;import java.lang.annotation.Documented;import java.lang.annotation.Retention;import java.lang.annotation.Target;import static java.lang.annotation.ElementType.*;import static java.lang.annotation.ElementType.TYPE_USE;import static java.lang.annotation.RetentionPolicy.RUNTIME;@Documented@Constraint(validatedBy = { StatusValueConstraintValidator.class })@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })@Retention(RUNTIME)@interface StatusValue { String message() default '{cn.jxj4869.valid.StatusValue.message}'; Class<?>[] groups() default { }; Class<? extends Payload>[] payload() default { }; int[] value() default { };}

自定義校驗(yàn)器

需要實(shí)現(xiàn)ConstraintValidator這個接口,第一個泛型是表示要校驗(yàn)?zāi)膫€注解,第二個泛型是要校驗(yàn)的數(shù)據(jù)的類型。

initialize是初始化方法 isValid校驗(yàn)方法,判斷是否校驗(yàn)成功

package cn.jxj4869.demo.valid;import javax.validation.ConstraintValidator;import javax.validation.ConstraintValidatorContext;import java.util.HashSet;import java.util.Set;public class StatusValueConstraintValidator implements ConstraintValidator<StatusValue,Integer> { private Set<Integer> set = new HashSet<>(); //初始化方法 @Override public void initialize(StatusValue constraintAnnotation) { int[] value = constraintAnnotation.value(); for (int val : value) { set.add(val); } } /** * 判斷是否校驗(yàn)成功 * @param value * @param context * @return */ @Override public boolean isValid(Integer value, ConstraintValidatorContext context) { return set.contains(value); }}

最后在resources目錄下添加一個ValidationMessages.properties文件

用來指定錯誤信息。

cn.jxj4869.valid.StatusValue.message=必須提交指定的值

UserBean

@Datapublic class User { @Null(groups = {AddGroup.class}) @NotNull(groups = {UpdateGroup.class}) private Integer id; @NotBlank(message = '用戶名不能為空',groups = {AddGroup.class,UpdateGroup.class}) private String username; @NotEmpty private String password; private String email; @StatusValue(value = {0,1},groups = {AddGroup.class,UpdateGroup.class}) private Integer status;}

SpringBoot后端進(jìn)行數(shù)據(jù)校驗(yàn)JSR303的使用詳解

SpringBoot后端進(jìn)行數(shù)據(jù)校驗(yàn)JSR303的使用詳解

常用注解匯總

注解 功能 @Null 對象必須為null @NotNull 對象必須不為null,無法檢查長度為0的字符串 @NotBlank 字符串必須不為Null,且去掉前后空格長度必須大于0 @NotEmpty 字符串必須非空 @Length(min = 1,max = 50) 字符串必須在指定長度內(nèi) @Range(min = 0,max = 100) 必須在指定范圍內(nèi) @AssertTrue 對象必須為true @AssertFalse 對象必須為false @Max(Value) 必須為數(shù)字,且小于或等于Value @Min(Value) 必須為數(shù)字,且大于或等于Value @DecimalMax(Value) 必須為數(shù)字( BigDecimal ),且小于或等于Value。小數(shù)存在精度 @DecimalMin(Value) 必須為數(shù)字( BigDecimal ),且大于或等于Value。小數(shù)存在精度 @Digits(integer,fraction) 必須為數(shù)字( BigDecimal ),integer整數(shù)精度,fraction小數(shù)精度 @Size(min,max) 對象(Array、Collection、Map、String)長度必須在給定范圍 @Email 字符串必須是合法郵件地址 @Past Date和Calendar對象必須在當(dāng)前時間之前 @Future Date和Calendar對象必須在當(dāng)前時間之后 @Pattern(regexp=“正則”) 字符串滿足正則表達(dá)式的值

到此這篇關(guān)于SpringBoot后端進(jìn)行數(shù)據(jù)校驗(yàn)JSR303的使用詳解的文章就介紹到這了,更多相關(guān)SpringBoot數(shù)據(jù)校驗(yàn)JSR303的使用內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 久久亚洲国产成人精品性色 | 午夜aaaa| 爱爱动态视频免费视频 | 国产精品福利在线观看免费不卡 | 日韩永久免费视频 | a级亚洲片精品久久久久久久 | 亚洲国产精品久久精品成人 | 国产精品成人va在线观看 | 国产在线观看一区 | 亚洲国产精品一区二区久 | 亚洲无线一二三四手机 | 欧美日批 | 久久99精品国产99久久6男男 | 青草草视频在线观看 | 亚洲综合另类 | xxxx国产片| 91丨国产| 免费国产一区二区三区 | 青草免费观看 | 国产亚洲欧美日韩综合另类 | 成人亚欧网站在线观看 | 一级片免费网址 | 一级国产在线观看高清 | 国产呦精品系列在线 | 国产高清在线观看麻豆 | 性做久久久久久久久男女 | 片在线观看视频免费 | 呦系列视频一区二区三区 | 你懂的最新网址 | 国产婷婷色| 一级做一级爱a做片性视频视频 | 国产精品深夜福利免费观看 | 6080亚洲| 亚洲一区二区三区在线免费观看 | 欧美一区二区三区免费看 | 久久久久久国产a免费观看黄色大片 | 成人无遮挡毛片免费看 | 青青在线精品视频 | 国产成人h综合亚洲欧美在线 | 日本欧美韩国专区 | 在线观看免费视频片 |