springboot全局异常处理,⼲掉trycatch。
写在前⾯
SpringBoot项⽬进⾏全局异常的处理是⾮常有必要的,不仅处理异常⽅便,⽽且可以优化代码,⼲掉try catch。本篇⽂章基于spring boot。
处理500异常
⾃定义基础接⼝类
public interface BaseErrorInfoInterface {
/** 错误码*/
int getResultCode();
/** 错误描述*/
String getResultMsg();
}
⾃定义枚举类
public enum CommonEnum implements BaseErrorInfoInterface {
// 数据操作错误定义
SUCCESS(200, "成功!"),
BODY_NOT_MATCH(400,"请求的数据格式不符!"),
SIGNATURE_NOT_MATCH(401,"请求的数字签名不匹配!"),
NOT_FOUND(404, "未到该资源!"),
INTERNAL_SERVER_ERROR(500, "服务器内部错误!"),
SERVER_BUSY(503,"服务器正忙,请稍后再试!")
;
/** 错误码 */
private int resultCode;
/** 错误描述 */
private String resultMsg;
CommonEnum(int resultCode, String resultMsg) {
}
@Override
public int getResultCode() {
return resultCode;
}
@Override
public String getResultMsg() {
return resultMsg;
}
}
⾃定义异常类
public class BusinessException extends RuntimeException  {
private static final long serialVersionUID = -4879677283847539655L;
private int errorCode;
private String errorMessage;
private String exceptionMessage;
private Exception exception;
public BusinessException(String errorMessage) {
super();
}
public BusinessException(int errorCode, String errorMessage) {
super();
}
public BusinessException(int errorCode, String errorMessage, Exception exception) {
super();
}
public BusinessException(String errorMessage, String exceptionMessage) {
super();
}
public String getExceptionMessage() {
return exceptionMessage;
}
public void setExceptionMessage(String exceptionMessage) {
}
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
}
public Exception getException() {
return exception;
}
public void setException(Exception exception) {
}
public BusinessException(int errorCode, String errorMessage, String exceptionMessage) {        super();
}
⾃定义数据格式
public class Result {
/**
* 响应代码
*/
private int code;
/**
* 响应消息
*/
private String message;
/**
* 响应结果
*/
private Object result;
public Result() {
}
public Result(BaseErrorInfoInterface errorInfo) {        de = ResultCode();
}
public int getCode() {
return code;
}
public void setCode(int code) {
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
}
/
**
* 成功
*
* @return
*/
public static Result success() {
return success(null);
spring到底是干啥的}
/**
* 成功
* @param data
* @return
*/
public static Result success(Object data) {
Result rb = new Result();
rb.setCode(ResultCode());        rb.setMessage(ResultMsg());        rb.setResult(data);
return rb;
}
/**
* 失败
*/
public static Result error(BaseErrorInfoInterface errorInfo) {        Result rb = new Result();
rb.ResultCode());
rb.ResultMsg());
rb.setResult(null);
return rb;
}
/**
* 失败
*/
public static Result error(int code, String message) {
Result rb = new Result();
rb.setCode(code);
rb.setMessage(message);
rb.setResult(null);
return rb;
}
/**
* 失败
*/
public static Result error(String message) {
Result rb = new Result();
rb.setCode(-1);
rb.setMessage(message);
rb.setResult(null);
return rb;
}
@Override
public String toString() {
JSONString(this);
}
}
⾃定义全局异常处理类
@ControllerAdvice
public class GlobalExceptionHandler {
/
**
* 处理⾃定义的业务异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value = BusinessException.class)
@ResponseBody
public Result bizExceptionHandler(HttpServletRequest req, BusinessException e){        println("未知异常!原因是:"+e.getErrorMessage());
(e.getErrorCode(),e.getErrorMessage());
}
/**
* 处理空指针的异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value =NullPointerException.class)
@ResponseBody
public Result exceptionHandler(HttpServletRequest req, NullPointerException e){        println("未知异常!原因是:"+e);
(CommonEnum.BODY_NOT_MATCH);
}
/**
* 处理其他异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value =Exception.class)
@ResponseBody
public Result exceptionHandler(HttpServletRequest req, Exception e){
(CommonEnum.INTERNAL_SERVER_ERROR);
}
}
控制层controller

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。