springboot 全局异常做法
springboot 配置全局异常,其实非常简单,只需要一个简单的配置类
package com.sinosig.ewp.wx.service.config; import com.sinosig.ewp.common.base.ResultDTO; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestControllerAdvice; /** * @author Mongo * @return */ @RestControllerAdvice @Slf4j public class GraceExceptionHandler { /** * 全局异常捕获 * @param e * @return */ @ExceptionHandler(Exception.class) @ResponseBody public Object returnException(Exception e) { log.error("error",e); return ResultsBean.build(500,"全局异常啦啦啦"); } }
重点是:这个配置类上的注解:@RestControllerAdvice ,方法上的注解 @ExceptionHandler(Exception.class)
之后程序里面,一旦发生了任何异常,都会走这里的异常方法,即可达到全局异常捕获!
==============
下面是 ResultsBean 统一返回格式工具类
package com.sinosig.ewp.wx.service.config; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; /** * api 统一返回前端数据格式工具类 * @author Mongo * @param * @return */ @Data @Component @Slf4j public class ResultsBean { private Integer status; private Integer change; private String msg; private Object data; public static ResultsBean ok(Object data) { return new ResultsBean(data); } public static ResultsBean ok() { return new ResultsBean(ResponseStatusEnum.SUCCESS); } public static ResultsBean answerOperation(int answer) { if(answer > 0) { return ResultsBean.ok(); } return ResultsBean.error(); } public ResultsBean() { this.status = 200; this.msg = "success"; } public ResultsBean(ResponseStatusEnum responseStatus) { this.status = responseStatus.status(); this.msg = responseStatus.msg(); } public ResultsBean(ResponseStatusEnum responseStatus, Object data) { this.status = responseStatus.status(); this.msg = responseStatus.msg(); this.data = data; } public ResultsBean(ResponseStatusEnum responseStatus, String msg) { this.status = responseStatus.status(); this.msg = msg; } public static ResultsBean error() { return new ResultsBean(ResponseStatusEnum.FAILED); } public static ResultsBean errorCustom(ResponseStatusEnum responseStatus) { return new ResultsBean(responseStatus); } public static ResultsBean exception(ResponseStatusEnum responseStatus) { return new ResultsBean(responseStatus); } public static ResultsBean build(Integer status, String msg) { return new ResultsBean(status, msg, null); } public static ResultsBean build(ResponseStatusEnum responseStatus) { return new ResultsBean(responseStatus.status(), responseStatus.msg(), null); } public ResultsBean(Integer status, String msg, Object data) { this.status = status; this.msg = msg; this.data = data; } public ResultsBean(Object data) { this.status = 200; this.msg = "success"; this.data = data; } }
package com.sinosig.ewp.wx.service.config; /** * 响应结果枚举,用于提供给JSON Result 统一格式化返回给前端 * @author Mongo */ public enum ResponseStatusEnum { SUCCESS(200, true, "success"),//操作成功 FAILED(500, false, "failure"),//操作失败 FAILED_All(500, false, "failure_all"),//操作失败 PARAM_NULL(101,false,"param_null"),//请求参数为空 DATA_NULL(102,false,"data_null"),//返回数据为空 OTHER_ERROR(103,false,"other_error"),//其他未知错误 TOKEN_EXPIRE(104,false,"token_expire"),//token 失效 ; private Integer status;//响应业务状态 private Boolean success;//调用是否成功 private String msg;//响应消息,可以为成功或者失败的消息 public void setMsg(String msg) { this.msg = msg; } private String addMsg;//附加消息,会拼接在msg后面 ResponseStatusEnum(Integer status, Boolean success, String msg) { this.status = status; this.success = success; this.msg = msg; } ResponseStatusEnum(Integer status, Boolean success, String msg, String addMsg) { this.status = status; this.success = success; this.msg = msg; this.addMsg = addMsg; } public Integer status() { return status; } public Boolean success() { return success; } public String msg() { return msg; } }