springboot 2.3.0 以后不会自动引入jar包,所以要添加以下maven
2.3以前则不需要引入maven包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
注册一个账号, 有账号+密码
前端会做一个判空,但是后端同时也要做一个判空,防止url直接访问
这时后端的代码通常是:
if(name!=null){
return "账号不能为空,请重新输入";
}
else if(password!=null){
return "密码不能为空,请重新输入";
}
这样就会显得特别low,而且极不美观
这时候就要用到一个注解@NotNull
简单举例说明:
@Data
public class UserInfo {
@NotNull(message = "姓名不能为null")
private String name;
@Max(value = 30,message = "年龄不能超过30")
private Integer age;
private Integer password;
private String sex;
}
Controller层:
简单举例说明
@RestController
public class TestController {
@RequestMapping("test1")
public Object test1(@Valid @RequestBody UserInfo userInfo,BindingResult result){
//判断有没有异常错误,如果有则返回默认消息
if (result.hasErrors()){
String defaultMessage = result.getFieldError().getDefaultMessage();
return defaultMessage;
}
//打印一下数据结构
System.out.println(userInfo);
//如果没有错误,返回注册成功
return "注册成功";
}
}
用postMan 传入json 参数,name有值,年龄超过30岁
返回我们的设定
name 为null 同理,会按顺序判断!
但是问题又来了,难道我们每个Controller层的方法里面都要写一个判断方法?
当然不,这时候就要用到全局异常类了
起一个类
@ControllerAdvice
public class ControllerException {
@ResponseBody
@ExceptionHandler(MethodArgumentNotValidException.class)
public Object handleValidException(MethodArgumentNotValidException e) {
//将错误信息返回给前台
return e.getBindingResult().getFieldError().getDefaultMessage();
}
}
为了有所区分,写下另一个访问方法
@RequestMapping("test2")
public Object test1(@Valid @RequestBody UserInfo userInfo){
System.out.println(userInfo);
return "注册成功";
}
到此结束,再也不用写一堆各种判空了
附上 部分注解
注解名称 | 注解内容 |
---|---|
@Null | 限制只能为null |
@NotNull | 限制必须不为null |
@DecimalMax(value)/@Max(value) | 限制必须为一个不大于指定值的数字 |
@DecimalMin(value) /@Min(value) | 限制必须为一个不小于指定值的数字 |
@AssertFalse/@AssertTrue | 限制必须为false/限制必须为true |
@NotEmpty | 验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0) |
@NotBlank | 验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格 |
二次更新:所有的Controller 都写这样的代码就要封装成异常类
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 :lsy
* @date :Created in 2020/7/23 10:13
* @modified By:
*/
@ControllerAdvice
public class ControllerException {
private final static String EXCEPTION_MSG_KEY = "Exception message : ";
@ResponseBody
@ExceptionHandler(MethodArgumentNotValidException.class)
public Object handleValidException(MethodArgumentNotValidException e) {
//日志记录错误信息
// log.error(Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage());
//将错误信息返回给前台
// return BaseResult.fail(500, Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage());
return e.getBindingResult().getFieldError().getDefaultMessage();
}
}
需要写BindingResult 也可以返回message
@RequestMapping("test2")
public Object test1(@Valid @RequestBody UserInfo userInfo){
System.out.println(userInfo);
return "注册成功";
}