介紹
在項(xiàng)目的過(guò)程中斋陪,對(duì)于參數(shù)的校驗(yàn)是必須的度宦,如果參數(shù)比較少的話我們可以直接通過(guò)代碼進(jìn)行校驗(yàn)戈抄,但是數(shù)據(jù)較大時(shí)再用這個(gè)方法就比較笨重了,接下來(lái)就該我們的主角Validation
閃亮登場(chǎng)了
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
</dependency>
</dependencies>
實(shí)際上在這里數(shù)據(jù)校驗(yàn)都是調(diào)用的javax.validation
,spring-boot-starter-web
中也包含了hibernate-validator
,有興趣的可以去翻翻文檔
User.java
package com.priv.gabriel.entity;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
/**
* Created with Intellij IDEA.
*
* @Author: Gabriel
* @Date: 2018-10-23
* @Description:
*/
@Data
public class User {
@NotBlank(message = "name不允許為空")
@Length(min = 2,max = 10,message = "你的長(zhǎng)度不對(duì)勁呀")
private String name;
@NotNull(message = "進(jìn)入未成年人入內(nèi)!")
@Min(18)
private int age;
@NotBlank(message = "拒絕黑戶")
private String address;
}
接下來(lái)只需要在Controller
層中使用@valid
進(jìn)行校驗(yàn)就可以了
package com.priv.gabriel.controller;
import com.priv.gabriel.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
/**
* Created with Intellij IDEA.
*
* @Author: Gabriel
* @Date: 2018-10-23
* @Description:
*/
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping("/check")
public String check(@Valid User user){
return "檢測(cè)完畢!沒(méi)有問(wèn)題";
}
}
用Rest Client
工具簡(jiǎn)單檢測(cè)一下
結(jié)果當(dāng)然是...
{
"timestamp": "2018-10-23T09:01:45.159+0000",
"status": 400,
"error": "Bad Request",
"errors": [{
"codes": ["Length.user.name", "Length.name", "Length.java.lang.String", "Length"],
"arguments": [{
"codes": ["user.name", "name"],
"arguments": null,
"defaultMessage": "name",
"code": "name"
}, 10, 2],
"defaultMessage": "你的長(zhǎng)度不對(duì)勁呀",
"objectName": "user",
"field": "name",
"rejectedValue": "",
"bindingFailure": false,
"code": "Length"
}, {
"codes": ["Min.user.age", "Min.age", "Min.int", "Min"],
"arguments": [{
"codes": ["user.age", "age"],
"arguments": null,
"defaultMessage": "age",
"code": "age"
}, 18],
"defaultMessage": "最小不能小于18",
"objectName": "user",
"field": "age",
"rejectedValue": 17,
"bindingFailure": false,
"code": "Min"
}, {
"codes": ["NotBlank.user.address", "NotBlank.address", "NotBlank.java.lang.String", "NotBlank"],
"arguments": [{
"codes": ["user.address", "address"],
"arguments": null,
"defaultMessage": "address",
"code": "address"
}],
"defaultMessage": "拒絕黑戶",
"objectName": "user",
"field": "address",
"rejectedValue": "",
"bindingFailure": false,
"code": "NotBlank"
}, {
"codes": ["NotBlank.user.name", "NotBlank.name", "NotBlank.java.lang.String", "NotBlank"],
"arguments": [{
"codes": ["user.name", "name"],
"arguments": null,
"defaultMessage": "name",
"code": "name"
}],
"defaultMessage": "name不允許為空",
"objectName": "user",
"field": "name",
"rejectedValue": "",
"bindingFailure": false,
"code": "NotBlank"
}],
"message": "Validation failed for object='user'. Error count: 4",
"path": "/user/check"
}
好了心肪,到這里可以看到我們的設(shè)置已經(jīng)生效了锅减,關(guān)于數(shù)據(jù)校驗(yàn)?zāi)?code>get到了嗎糖儡?
項(xiàng)目下載地址