一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - SpringBoot后端進(jìn)行數(shù)據(jù)校驗JSR303的使用詳解

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

2021-08-20 11:35jiangxiaoju Java教程

這篇文章主要介紹了SpringBoot后端進(jìn)行數(shù)據(jù)校驗JSR303的使用詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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

簡介

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

項目創(chuàng)建

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

使用的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)在新一點的springboot版本中,需要單獨引入。在老版本是默認(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;

@Data
public 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.*;

@Controller
public 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)的檢驗方式

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

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

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

使用JSR303

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

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

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

步驟如下:

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

2、開啟校驗功能@Valid。

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

常用的校驗注解在文末

package cn.jxj4869.demo.entity;

import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotNull;

@Data
public 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)校驗失敗后,會出現(xiàn)如下錯誤。并且會給出默認(rèn)的提示信息。

SpringBoot后端進(jìn)行數(shù)據(jù)校驗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ù)校驗JSR303的使用詳解

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

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

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

指定message的值即可。

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

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

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

當(dāng)校驗出錯時,會默認(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、獲取校驗的錯誤結(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)校驗的地方多了,每個方法里面都加上這么個異常處理,會讓代碼很臃腫。

不知道你們是否還記得,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、獲取校驗的錯誤結(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);

 }
}

錯誤異常類型補充

校驗出錯的時候,會拋出兩種異常

org.springframework.validation.BindException

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

org.springframework.web.bind.MethodArgumentNotValidException

使用@validated校驗的時候拋出的

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

 @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è)務(wù)場景下,校驗規(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 { };
	........
}

首先定義兩個接口AddGroupUpdateGroup,不需要做任何實現(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)行校驗,不能為空。
  • password屬性,當(dāng)校驗的時候指定分組的話,會不起作用,因為沒有給它指定校驗的分組
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;


@Data
public 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注解,指定校驗的分組

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

  return null;
 }

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

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

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

自定義校驗

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

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

新建一個@StatusValue注解。

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

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

使用@Constraint注解指定該注解的校驗器

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

自定義校驗器

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

  • initialize是初始化方法
  • isValid校驗方法,判斷是否校驗成功
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);
  }

 }
 /**
  * 判斷是否校驗成功
  * @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

@Data
public 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ù)校驗JSR303的使用詳解

SpringBoot后端進(jìn)行數(shù)據(jù)校驗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ù)校驗JSR303的使用詳解的文章就介紹到這了,更多相關(guān)SpringBoot數(shù)據(jù)校驗JSR303的使用內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/qq_43058685/article/details/114381502

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 性欧美videofree中文字幕 | 和肥岳在厨房激情 | 毛片在线网址 | 校花在公车上被内射好舒 | 欧美视频黑鬼大战白妞 | 精品免费国产 | 欧美视频黑鬼大战白妞 | 538精品视频 | 欧美日韩一区二区综合 | np高h疯狂黄暴宫口 narutomanga玖辛奈之乳 | 精品日韩一区二区三区 | 日本人作爰啪啪全过程 | 美女在线看永久免费网址 | 国产成人lu在线视频 | 午夜勾魂曲 | 亚洲精品国产综合久久一线 | 近亲乱中文字幕 | 小仙夜晚慰自催眠mp3护士篇 | 亚洲AV无码国产精品色午夜情 | 88av免费观看 | 俺来操| 423hk四虎| 97菊爱网| jj免费视频| 青草精品 | 国产精品www夜色影视 | 国产久草在线 | 91天堂一区二区 | 亚洲美女aⅴ久久久91 | 希岛爱理作品在线观看 | 草留色区| av在线色| 添逼逼视频 | 久久中文字幕综合不卡一二区 | 亚洲精品 欧美 | 欧美精品综合一区二区三区 | 久久国产精品高清一区二区三区 | 好深快点再快点好爽视频 | 国产精品九九免费视频 | 国产精品香蕉一区二区三区 | 亚洲AV 无码AV 中文字幕 |