方法一:
CodeMsg.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| package com.example.testspringboot.exption;
public class BizException extends RuntimeException{ private static final long serialVersionUID = 1L;
protected String errorCode;
protected String errorMsg;
public BizException() { super(); }
public BizException(BaseErrorInfoInterface errorInfoInterface) { super(errorInfoInterface.getResultCode()); this.errorCode = errorInfoInterface.getResultCode(); this.errorMsg = errorInfoInterface.getResultMsg(); }
public BizException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) { super(errorInfoInterface.getResultCode(), cause); this.errorCode = errorInfoInterface.getResultCode(); this.errorMsg = errorInfoInterface.getResultMsg(); }
public BizException(String errorMsg) { super(errorMsg); this.errorMsg = errorMsg; }
public BizException(String errorCode, String errorMsg) { super(errorCode); this.errorCode = errorCode; this.errorMsg = errorMsg; }
public BizException(String errorCode, String errorMsg, Throwable cause) { super(errorCode, cause); this.errorCode = errorCode; this.errorMsg = errorMsg; }
public String getErrorCode() { return errorCode; }
public void setErrorCode(String errorCode) { this.errorCode = errorCode; }
public String getErrorMsg() { return errorMsg; }
public void setErrorMsg(String errorMsg) { this.errorMsg = errorMsg; }
@Override public Throwable fillInStackTrace() { return this; }
}
|
GlobalExceptionHandler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package com.example.testspringboot.exption; import org.mybatis.logging.Logger; import org.mybatis.logging.LoggerFactory; import org.springframework.validation.BindException; import org.springframework.validation.ObjectError; 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;
@ControllerAdvice @ResponseBody public class GlobalExceptionHandler { private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(value = BindException.class) public Result exceptionHandler(BindException e){ Result result = new Result(); if(e instanceof BindException){ BindException exception=(BindException) e; List<ObjectError> errors=exception.getAllErrors(); String msg=errors.get(0).getDefaultMessage(); return Result.error(CodeMsg.SERVER_BIND_ERROR.fillArgs(msg)); } return Result.error(CodeMsg.SERVER_ERROR); }
}
|
Result.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.example.testspringboot.exption;
import jdk.nashorn.internal.objects.annotations.Constructor; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;
@Data @AllArgsConstructor @NoArgsConstructor public class Result { private Integer status; private String msg;
public static Result error(RuntimeException e) { Result result = new Result(); result.setMsg(e.getMessage()); return result; }
public static Result error(CodeMsg codeMsg) { Result result = new Result(); result.setMsg(codeMsg.getMsg()); result.setStatus(codeMsg.getCode()); return result; } }
|
方法二:
Result.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| @AllArgsConstructor @NoArgsConstructor @Data public class Result<T> { private String code;
private String msg;
private T data;
public static Result error(BaseErrorInfo errorInfo) { Result rb = new Result(); rb.setCode(errorInfo.getResultCode()); rb.setMsg(errorInfo.getGetResultMsg()); rb.setData(null); return rb; }
public static Result error(String code, String message) { Result rb = new Result(); rb.setCode(code); rb.setMsg(message); rb.setData(null); return rb; }
}
|
BaseErrorInfoInterface.java
1 2 3 4 5 6 7 8 9 10
| public interface BaseErrorInfo {
String getResultCode();
String getGetResultMsg(); }
|
CommonEnum.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public enum CommonEnum implements BaseErrorInfo{
SUCCESS("200","成功"), RUNTIME_ERROR("5001", "自定义的错误"), NO_KNOW_ERROR("5002", "未知的异常信息") ;
private String resultCode; private String resultMsg;
CommonEnum(String resultCode, String resultMsg) { this.resultCode = resultCode; this.resultMsg = resultMsg; }
@Override public String getResultCode() { return resultCode; }
@Override public String getGetResultMsg() { return resultMsg; } }
|
BizException.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class BizException extends RuntimeException{
private String errorCode; private String errorMsg;
public BizException() { }
public BizException(BaseErrorInfo errorInfoInterface) { super(errorInfoInterface.getResultCode()); this.errorCode = errorInfoInterface.getResultCode(); this.errorMsg = errorInfoInterface.getGetResultMsg(); }
public BizException(String errorCode, String errorMsg) { super(errorCode); this.errorCode = errorCode; this.errorMsg = errorMsg; } }
|
GlobalExceptionHandler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @ControllerAdvice public class GlobalException {
@ExceptionHandler(value = RuntimeException.class) @ResponseBody public Result runtimeException(RuntimeException e) { return Result.error(CommonEnum.RUNTIME_ERROR); }
@ExceptionHandler(value =Exception.class) @ResponseBody public Result exceptionHandler(HttpServletRequest req, Exception e){ return Result.error(CommonEnum.NO_KNOW_ERROR); }
}
|
注: return Result.error(CommonEnum.NO_KNOW_ERROR); // 枚举类是BaseErrorInfo接口的具体子类,所以这里可以写接口实现类的参数
测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| @RestController public class TestController { @Autowired UserService userService;
@GetMapping("/error1") public int error_test0() { return 4/0; }
@GetMapping("/error2") public int error_test1() { return userService.test11(11); }
@GetMapping("/error3") public int error_test2() { if (true) { throw new BizException("5004", "抛出的自定义异常"); } return 1; }
@GetMapping("/error4") public int error_test3() { if (true) { throw new BizException("5005", "抛出的自定义异常11"); } return 1; } }
|