JSR303需要的依赖
<!-- jsr 303 -->
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.7.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<!-- jsr 303 -->
全局异常处理器
package com.hzy.work.aspect;
import com.hzy.work.base.HzyException;
import com.hzy.work.base.JsonResultUtil;
import com.hzy.work.base.Result;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
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;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* @author hzy
* @date 2020/3/24 10:34
* @description 全局异常处理器
*/
@ControllerAdvice
@ResponseBody
public class ExceptionAspect {
@ExceptionHandler(value=Exception.class)
public Result<Object> exceptionHandler(HttpServletRequest request, Exception e){
System.out.println("进入全局异常处理器");
e.printStackTrace();
if (e instanceof MethodArgumentNotValidException){
//JSR303异常
//这里需要注意,我这里是判断jsr303抛出来的异常是MethodArgumentNotValidException而我在网上看到很多都是BindException,这里原因在哪我也不知道,如果有知道的也可以和我分享下,谢谢啦
MethodArgumentNotValidException bindException = (MethodArgumentNotValidException) e;
return JsonResultUtil.error(bindException.getBindingResult().getAllErrors().iterator().next().getDefaultMessage());
}else if (e instanceof HzyException){
//自定义异常
HzyException myException = (HzyException)e;
return JsonResultUtil.error(myException.getMsg());
}else {
//系统bug
return JsonResultUtil.error("系统内部异常,请联系管理员");
}
}
}
Controller测试
@RequestMapping("test")
public Result test(@Valid @RequestBody Student student){
Result<Student> result = new Result<>();
System.out.println("进入test");
result.setData(student);
return result;
}
/**
* 测试自定义异常
* @return
*/
@RequestMapping("test1")
public Result test1(){
Result<String> result = new Result<>();
System.out.println("进入test 1");
try{
int i = 5 / 0;
}catch (Exception e){
throw new HzyException(Constants.ERROR,"除数不能为0");
}
return result;
}
package com.hzy.work.pojo;
import lombok.Data;
import javax.persistence.Entity;
import javax.validation.constraints.*;
import java.util.Date;
/**
* @author hzy
* @date 2020/3/23 16:11
* @description
*/
@Entity
@Data
public class Student {
@NotBlank(message = "字段no不能为空")
@Size(min = 6,max = 32,message = "字段no的长度必须在6-32位")
private String no;
@NotBlank(message = "字段name不能为空")
@Size(min = 2,max = 32,message = "字段name的长度必须在2-32位")
private String name;
@Min(value = 0 ,message = "字段age必须大于等于0")
@Max(value = 150,message = "字段age必须小于等于150")
@NotNull(message = "字段age不能为空")
private Integer age;
@Email(message = "字段email请按照邮箱格式填写")
private String email;
@Past(message = "字段birthday必须是今天之前")
private Date birthday;
@AssertTrue(message = "字段status必须是true")
private Boolean status;
}