- 案例1
@RestController
public class UserInfoHandler {
/**
* 编程式校验
* @param userInfo
* @return
*/
@GetMapping("/addUser")
public String addUser(UserInfo userInfo) {
List<String> result = ValidationUtil.valid(userInfo);
if (result.size() > 0) {
System.out.println(result);
return "校验不成功";
}else {
return "添加成功";
}
}
}
- 案例2
@RestController
public class UserInfoHandler {
/**
* springmvc 环境中校验一般可以声明式,这种更简单,springmvc也更推荐
* @param userInfo
*/
@GetMapping("/addUser2")
public String addUser2(@Valid UserInfo userInfo, BindingResult bindingResult) {
if (bindingResult.hasErrors()) { // 判断是不是有不满足约束的
List<ObjectError> allErrors = bindingResult.getAllErrors();
for (ObjectError error : allErrors) {
System.out.println(error.getObjectName() +"::" + error.getDefaultMessage());
}
// 获取没通过校验的字段详情
List<FieldError> fieldErrors = bindingResult.getFieldErrors();
for (FieldError fieldError : fieldErrors) {
System.out.println(fieldError.getField() + ":" +fieldError.getDefaultMessage()
+",当前没通过校验规则的值是:"+fieldError.getRejectedValue());
}
return "校验不通过";
}
return "添加成功";
}
}
- 案例3:分组校验
@RestController
public class UserInfoHandler {
/**
* 测试@Validated的分组校验
* @param userInfo
* @param bindingResult
* @return
*/
@GetMapping("/addUser3")
public String addUser3(@Validated(value = {UserInfo.Add.class, Default.class})
UserInfo userInfo, BindingResult bindingResult) {
if (bindingResult.hasErrors()) { // 判断是不是有不满足约束的
List<ObjectError> allErrors = bindingResult.getAllErrors();
for (ObjectError error : allErrors) {
System.out.println(error.getObjectName() +"::" + error.getDefaultMessage());
}
// 获取没通过校验的字段详情
List<FieldError> fieldErrors = bindingResult.getFieldErrors();
for (FieldError fieldError : fieldErrors) {
System.out.println(fieldError.getField() + ":" +fieldError.getDefaultMessage()
+",当前没通过校验规则的值是:"+fieldError.getRejectedValue());
}
return "校验不通过";
}
return "添加成功";
}
}
- 案例4:异常处理
@ControllerAdvice
public class RoadJavaControllerAdvice {
// @Validated 注解写在方法上的时候会报这个异常
@ExceptionHandler(BindException.class)
@ResponseBody
public String handleEx(BindException e) {
List<FieldError> fieldErrors = e.getFieldErrors();
StringBuilder sb = new StringBuilder("这是RoadJavaControllerAdvice,");
for (FieldError fe: fieldErrors) {
sb.append("属性:").append(fe.getField())
.append("校验不通过,原因:").append(fe.getDefaultMessage())
.append(";");
}
return sb.toString();
}
}
@RestController
public class UserInfoHandler {
@GetMapping("/addUser4")
public String addUser4(@Validated(value = {UserInfo.Add.class, Default.class})
UserInfo userInfo) {
return "addUser4添加成功";
}
}
- 案例5
@ControllerAdvice
public class RoadJavaControllerAdvice {
// @Validated 注解写在方法上的时候会报这个异常
@ExceptionHandler(BindException.class)
@ResponseBody
public String handleEx(BindException e) {
List<FieldError> fieldErrors = e.getFieldErrors();
StringBuilder sb = new StringBuilder("这是RoadJavaControllerAdvice,");
for (FieldError fe: fieldErrors) {
sb.append("属性:").append(fe.getField())
.append("校验不通过,原因:").append(fe.getDefaultMessage())
.append(";");
}
return sb.toString();
}
// @Validated 注解写在类上的时候会报这个异常
@ExceptionHandler(ConstraintViolationException.class)
@ResponseBody
public List<String> handleEx(ConstraintViolationException e) {
Set<ConstraintViolation<?>> set = e.getConstraintViolations();
List<String> list = set.stream().map(v -> "属性:" + v.getPropertyPath() +
",属性的值:"
+ v.getInvalidValue() + ",校验不通过的提示信息:" + v.getMessage()
+",消息模板:"+v.getMessageTemplate()) // el表达式还没被解析成值的时候
.collect(Collectors.toList());
return list;
}
// 处理所有的异常
@ExceptionHandler(Exception.class)
@ResponseBody
public String handleEx(Exception e) {
return e.getMessage();
}
}
@RestController
@Validated // 表示整个类都启用校验,如果碰到入参含有bean validation 注解的话,就会自动校验
public class UserInfoHandler {
@GetMapping("/getByName")
public String getByName(@NotNull String name) {
return name+"ok";
}
}
标签:return,springboot,校验,public,整合,class,append,String
From: https://www.cnblogs.com/chniny/p/16767410.html