开发过程中,后台的参数校验是必不可少的,spring帮我们很好的解决了这个问题:
1.引入pom文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-web中依赖hibernate-validator
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
2.controller类添加校验注解
@RequestMapping(value = "/save", method = RequestMethod.POST)
public Response add(@Validated(value = AddVO.Default.class) @RequestBody AddVO vo) {
System.out.println("====================");
}
3.构建接收参数类 并在字段上添加验证类
public class AddVO implements Serializable{
public interface Default {
}
public interface Update {
}
/**
* serialVersionUID
*/
private static final long serialVersionUID = 6905396242189398992L;
/**
* 主键
*/
@NotNull(message="id 不能为空", groups = Update.class)
private Long id;
/**
* 组织id
**/
private String orgId;
/**
*
*/
@NotBlank(message=" 名称不能为空", groups = {Update.class,Default.class})
private java.lang.String name;
/**
*
*
*/
@NotNull(message="类型不能为空", groups = {Update.class,Default.class})
private int type;
}
4.定义全局异常处理类
/**
* 全局异常处理器
*/
@ControllerAdvice
public class GlobalExceptionHandler {
/**
* 用来处理bean validation异常
* @param ex
* @return
*/
@ExceptionHandler(ConstraintViolationException.class)
@ResponseBody
public Response<Object> resolveConstraintViolationException(ConstraintViolationException ex){
Response<Object> result = new Response<>();
Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations();
if(!CollectionUtils.isEmpty(constraintViolations)){
StringBuilder msgBuilder = new StringBuilder();
for(@SuppressWarnings("rawtypes") ConstraintViolation constraintViolation :constraintViolations){
msgBuilder.append(constraintViolation.getMessage()).append(",");
}
String errorMessage = msgBuilder.toString();
if(errorMessage.length()>1){
errorMessage = errorMessage.substring(0,errorMessage.length()-1);
}
result.failure(errorMessage);
return result;
}
result.failure(ex.getMessage());
return result;
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public Response<Object> resolveMethodArgumentNotValidException(MethodArgumentNotValidException ex){
Response<Object> result = new Response<>();
List<ObjectError> objectErrors = ex.getBindingResult().getAllErrors();
if(!CollectionUtils.isEmpty(objectErrors)) {
StringBuilder msgBuilder = new StringBuilder();
for (ObjectError objectError : objectErrors) {
msgBuilder.append(objectError.getDefaultMessage()).append(",");
}
String errorMessage = msgBuilder.toString();
if (errorMessage.length() > 1) {
errorMessage = errorMessage.substring(0, errorMessage.length() - 1);
}
result.failure(errorMessage);
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
return result;
}
result.failure(ex.getMessage());
return result;
}