你的項(xiàng)目中是否用到參數(shù)校驗(yàn)?zāi)匕の瘢€是說你就沒有考慮入?yún)榭盏那闆r纯丸。今天教大家基于SpringBoot的注解來判斷入?yún)⑹欠裾_晌畅。
1但指、首先創(chuàng)建項(xiàng)目所需的Bean
package com.ifilldream.check_lean.demo.bean;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* @author RickSun && iFilldream
*/
@Data
public class User {
//message可以表達(dá)的再委婉一點(diǎn),小編就直接剛了
@NotNull ( message = "用戶姓名不得為null")
@NotEmpty( message = "用戶姓名不得為空")
private String name;
@NotNull ( message = "用戶年齡不得為空")
@Max(value = 130,message = "年齡不得大于130歲")
@Max(value = 1,message = "年齡不得小于1歲")
private Integer age;
@NotNull ( message = "用戶手機(jī)不得為null")
@NotEmpty( message = "用戶手機(jī)不得為空")
@Length( min = 11,max = 11,message = "用戶手機(jī)長度必須為11位")
private String telephone;
}
2抗楔、創(chuàng)建Controller
package com.ifilldream.check_lean.demo.controller;
import com.ifilldream.check_lean.demo.bean.User;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
/**
* @author RickSun && iFilldream
*/
@RestController()
@RequestMapping("/ifilldream/user")
public class CheckController {
@PostMapping("/add")
public String addUser(@RequestBody @Valid User user){
//Do something
return "ok";
}
}
注意棋凳,此處一定要加入@Valid注解用于驗(yàn)證。
3连躏、測試效果
我們可以看到錯(cuò)誤提示是非常詳細(xì)的剩岳,但是這種返回格式并不是我們想要的,此時(shí)就需要統(tǒng)一攔截處理反粥,轉(zhuǎn)換成項(xiàng)目所需的返回格式了卢肃。
4、配置攔截
package com.ifilldream.check_lean.demo.filter;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author RickSun && iFilldream
*/
@ControllerAdvice
@ResponseBody
public class ErrorHandler {
@ExceptionHandler(Exception.class)
public String handleExption(Exception e){
String msg = null;
if(e instanceof MethodArgumentNotValidException) {
//將具體的錯(cuò)誤提示提取出來
msg = ((MethodArgumentNotValidException) e).getBindingResult().getFieldError().getDefaultMessage();
}else {
msg = e.getMessage();
}
return msg;
}
}
此處返回的是String,可以根據(jù)改成項(xiàng)目統(tǒng)一的返回類才顿。有人就會(huì)問了莫湘,這個(gè)方法的反射效率會(huì)不會(huì)很低,其實(shí)并不低的郑气,可以放心使用幅垮。
這里只簡單的介紹了入?yún)⑿r?yàn),更多校驗(yàn)請參考以下注解:
@Null 被注釋的元素必須為null
@NotNull 被注釋的元素不能為null
@AssertTrue 被注釋的元素必須為true
@AssertFalse 被注釋的元素必須為false
@Min(value) 被注釋的元素必須是一個(gè)數(shù)字尾组,其值必須大于等于指定的最小值
@Max(value) 被注釋的元素必須是一個(gè)數(shù)字忙芒,其值必須小于等于指定的最大值
@DecimalMin(value) 被注釋的元素必須是一個(gè)數(shù)字,其值必須大于等于指定的最小值
@DecimalMax(value) 被注釋的元素必須是一個(gè)數(shù)字讳侨,其值必須小于等于指定的最大值
@Size(max,min) 被注釋的元素的大小必須在指定的范圍內(nèi)呵萨。
@Digits(integer,fraction) 被注釋的元素必須是一個(gè)數(shù)字,其值必須在可接受的范圍內(nèi)
@Past 被注釋的元素必須是一個(gè)過去的日期
@Future 被注釋的元素必須是一個(gè)將來的日期
@Pattern(value) 被注釋的元素必須符合指定的正則表達(dá)式跨跨。
@Email 被注釋的元素必須是電子郵件地址
@Length 被注釋的字符串的大小必須在指定的范圍內(nèi)
@NotEmpty 被注釋的字符串必須非空
@Range 被注釋的元素必須在合適的范圍內(nèi)
統(tǒng)一首發(fā)平臺為微信公眾號"輕夢致新"潮峦,搜索關(guān)注公眾號囱皿,第一時(shí)間閱讀最新內(nèi)容。