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;
}