最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring boot輸入數(shù)據(jù)校驗(validation)的實現(xiàn)過程

 更新時間:2021年09月22日 09:11:39   作者:吳吃辣  
web項目中,用戶的輸入總是被假定不安全不正確的,在被處理前需要做校驗。本文介紹在spring boot項目中實現(xiàn)數(shù)據(jù)校驗的過程,通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧

項目內(nèi)容

實現(xiàn)一個簡單的用戶注冊接口,演示怎樣進行數(shù)據(jù)校驗。

要求

  • JDK1.8或更新版本
  • Eclipse開發(fā)環(huán)境

如沒有開發(fā)環(huán)境,可參考 [spring boot 開發(fā)環(huán)境搭建(Eclipse)]。

項目創(chuàng)建

創(chuàng)建spring boot項目

打開Eclipse,創(chuàng)建spring boot的spring starter project項目,選擇菜單:File > New > Project ...,彈出對話框,選擇:Spring Boot > Spring Starter Project,在配置依賴時,勾選web,完成項目創(chuàng)建。

項目依賴

pom.xml的內(nèi)容

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.qikegu</groupId>
	<artifactId>springboot-validation-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-validation-demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

代碼實現(xiàn)

項目目錄結構如下圖,添加了幾個類,下面將詳細介紹。

創(chuàng)建RegisterRequest

用戶注冊時,要求輸入手機號、密碼、昵稱,創(chuàng)建RegisterRequest類來接受前端傳過來的數(shù)據(jù),同時校驗數(shù)據(jù),校驗都是通過注解的方式實現(xiàn)。

public class RegisterRequest {
	
	@SuppressWarnings("unused")
	private static final org.slf4j.Logger log = LoggerFactory.getLogger(RegisterRequest.class);
	
	@NotNull(message="手機號必須填")
	@Pattern(regexp = "^[1]([3][0-9]{1}|59|58|88|89)[0-9]{8}$", message="賬號請輸入11位手機號") // 手機號
	private String mobile;
	
	@NotNull(message="昵稱必須填")
	@Size(min=1, max=20, message="昵稱1~20個字")
	private String nickname;
	
    @NotNull(message="密碼必須填")
    @Size(min=6, max=16, message="密碼6~16位")
	private String password;

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
    	
}

說明

RegisterRequest類有3個成員變量:mobile,nickname, password,這些變量都帶有注解:

  • @NotNull 表示必填
  • @Size 字符串長度必須符合指定范圍
  • @Pattern 輸入字符串必須匹配正則表達式

創(chuàng)建控制器

我們創(chuàng)建AuthController控制器類,實現(xiàn)一個用戶注冊的接口:

package com.qikegu.demo.controller;

import javax.validation.Valid;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.qikegu.demo.common.util.MiscUtil;
import com.qikegu.demo.common.util.Result;
import com.qikegu.demo.model.RegisterRequest;

@RestController
@RequestMapping("/auth")
public class AuthController {
	
    @RequestMapping(value = "/register", method = RequestMethod.POST, produces="application/json")
    public ResponseEntity<Result> register(
    	@Valid @RequestBody RegisterRequest register, 
    	BindingResult bindingResult
    ) {
		if(bindingResult.hasErrors()) {	
			//rfc4918 - 11.2. 422: Unprocessable Entity
//			res.setStatus(422);
//			res.setMessage("輸入錯誤");
//			res.putData("fieldErrors", bindingResult.getFieldErrors());
			
			Result res1 = MiscUtil.getValidateError(bindingResult);
			
			return new ResponseEntity<Result>(res1, HttpStatus.UNPROCESSABLE_ENTITY);
		}
    	
		Result res = new Result(200, "ok");
        return ResponseEntity.ok(res);
    }
}

說明

方法register有2個參數(shù)

  • @Valid @RequestBody RegisterRequest register@RequestBody 表明輸入來自請求body,@Valid表明在綁定輸入時作校驗
  • BindingResult bindingResult 這個參數(shù)存放校驗結果

輔助類 MiscUtil,Result

直接返回bindingResult過于復雜,使用MiscUtil.getValidateError簡化校驗結果

static public Result getValidateError(BindingResult bindingResult) {
		
		if(bindingResult.hasErrors() == false) 
			return null;
		
		Map<String,String> fieldErrors = new HashMap<String, String>();
		
		for(FieldError error : bindingResult.getFieldErrors()){
			fieldErrors.put(error.getField(), error.getCode() + "|" + error.getDefaultMessage());
		}
		
		Result ret = new Result(422, "輸入錯誤"); //rfc4918 - 11.2. 422: Unprocessable Entity
		ret.putData("fieldErrors", fieldErrors);
		
		return ret;
	}

Result是結果封裝類,[spring boot 接口返回值封裝] 那一篇中已經(jīng)介紹過。

運行

Eclipse左側,在項目根目錄上點擊鼠標右鍵彈出菜單,選擇:run as -> spring boot app 運行程序。 打開Postman訪問接口,運行結果如下:

輸入錯誤的情況

輸入正確的情況

總結

完整代碼

到此這篇關于spring boot輸入數(shù)據(jù)校驗(validation)的文章就介紹到這了,更多相關spring boot數(shù)據(jù)校驗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java內(nèi)部類詳解

    Java內(nèi)部類詳解

    內(nèi)部類在 Java 里面算是非常常見的一個功能了,在日常開發(fā)中我們肯定多多少少都用過,這里總結一下關于 Java 中內(nèi)部類的相關知識點和一些使用內(nèi)部類時需要注意的點。
    2020-02-02
  • 淺析Android系統(tǒng)中HTTPS通信的實現(xiàn)

    淺析Android系統(tǒng)中HTTPS通信的實現(xiàn)

    這篇文章主要介紹了淺析Android系統(tǒng)中HTTPS通信的實現(xiàn),實現(xiàn)握手的源碼為Java語言編寫,需要的朋友可以參考下
    2015-07-07
  • springboot?通過博途獲取plc點位的數(shù)據(jù)代碼實現(xiàn)

    springboot?通過博途獲取plc點位的數(shù)據(jù)代碼實現(xiàn)

    這篇文章主要介紹了springboot?通過博途獲取plc點位的數(shù)據(jù)的代碼實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • Mybatis攔截器的實現(xiàn)介紹

    Mybatis攔截器的實現(xiàn)介紹

    MyBatis提供了一種插件(plugin)的功能,雖然叫做插件,但其實這是攔截器功能。MyBatis 允許你在已映射語句執(zhí)行過程中的某一點進行攔截調用。下面通過本文給大家介紹Mybatis攔截器知識,感興趣的朋友一起看看吧
    2016-10-10
  • 詳解Java的堆內(nèi)存與棧內(nèi)存的存儲機制

    詳解Java的堆內(nèi)存與棧內(nèi)存的存儲機制

    這篇文章主要介紹了Java的堆內(nèi)存與棧內(nèi)存的存儲機制,包括JVM的內(nèi)存優(yōu)化和GC等相關方面內(nèi)容,需要的朋友可以參考下
    2016-01-01
  • SpringBoot簡單的SpringBoot后端實例

    SpringBoot簡單的SpringBoot后端實例

    這篇文章主要介紹了SpringBoot簡單的SpringBoot后端實例,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Spring-AOP-ProceedingJoinPoint的使用詳解

    Spring-AOP-ProceedingJoinPoint的使用詳解

    這篇文章主要介紹了Spring-AOP-ProceedingJoinPoint的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Java判斷所給年份是平年還是閏年

    Java判斷所給年份是平年還是閏年

    這篇文章主要為大家詳細介紹了Java判斷所給年份是平年還是閏年,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 使用1招搞定maven打包空間不足的問題

    使用1招搞定maven打包空間不足的問題

    這篇文章主要介紹了使用1招搞定maven打包空間不足的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • Java如何實現(xiàn)簡單后臺訪問并獲取IP

    Java如何實現(xiàn)簡單后臺訪問并獲取IP

    這篇文章主要介紹了Java如何實現(xiàn)簡單后臺訪問并獲取IP,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-10-10

最新評論

会宁县| 苍山县| 新巴尔虎左旗| 阿尔山市| 中江县| 阳西县| 凉城县| 司法| 西安市| 宜兰市| 陆良县| 滁州市| 财经| 光山县| 巩留县| 南城县| 普兰县| 桂东县| 洛隆县| 大兴区| 珠海市| 巴中市| 天气| 阿城市| 丰县| 武川县| 平罗县| 科尔| 花莲县| 肥东县| 蛟河市| 通化市| 衡山县| 滕州市| 英超| 兴宁市| 且末县| 彭州市| 乐清市| 罗城| 新密市|