最恶心的事莫过于,后台出现异常返回的是一坨错误码,如下图。前端的人一定很抓狂。今天我们看看Java是如何处理异常的,更友好的返回错误信息。就拿做小程序接口时的一个demo。
当然我们是基于spring简单的使用,首先我们了解用到的几个注解:
@ControllerAdvice 对Controller的一个增强,可以对异常进行统一处理
@ExceptionHandle 捕获所有控制器里面的异常
首先定义个全局的异常处理类:
package com.fms.config.exception;
import com.fms.config.Constant;
import com.fms.config.exception.customer.wx.WxException;
import com.fms.config.exception.customer.WxJsCodeEmptyException;
import com.fms.http.BaseResponseJson;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class ExceptionHandler {
private static final Logger logger = LogManager.getLogger(ExceptionHandler.class);
/**
* 处理自定义的业务异常
*
* @return
*/
@org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
@ResponseBody
public BaseResponseJson handerException(Exception e) {
logger.info(e);
return BaseResponseJson.fail(Constant.EX_UNKNOW_EXCEPTION, "未知错误");
}
/**
* 处理微信相关的异常
*
* @return
*/
@org.springframework.web.bind.annotation.ExceptionHandler(WxException.class)
@ResponseBody
public BaseResponseJson handerWxException(WxException e) {
if (e instanceof WxJsCodeEmptyException) {//微信小程序登录时获取的 js_code 出错
return BaseResponseJson.fail(Constant.WX_JS_CODE_EMPTY, Constant.WX_JS_CODE_EMPTY_MSG);
} else {
}
logger.info(e);
return BaseResponseJson.fail(Constant.EX_UNKNOW_EXCEPTION, "微信API出错");
}
/**
* 其他业务异常
*/
}
WxException是自定义的一个微信异常类,WxException是微信相关的异常的基类,如:WxJsCodeEmptyException 。BaseResponseJson是返回给前端的信息。
package com.fms.http;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fms.enum_pack.ResponseCode;
import java.io.Serializable;
/**
* Java封装接口返回统一格式
*/
public class BaseResponseJson implements Serializable {
private static final long serialVersionUID = -3948389268046368059L;
@JsonProperty()
private String code;
@JsonProperty()
private String msg;
@JsonProperty()
private Object data;
public static BaseResponseJson success( ) {
return resultData(ResponseCode.SUCCESS.val(), ResponseCode.SUCCESS.msg());
}
public static BaseResponseJson success(Object data) {
return resultData(ResponseCode.SUCCESS.val(), ResponseCode.SUCCESS.msg(), data);
}
public static BaseResponseJson success(Object data, String msg) {
return resultData(ResponseCode.SUCCESS.val(), msg, data);
}
public static BaseResponseJson fail(String msg) {
return resultData(ResponseCode.ERROR.val(), msg);
}
public static BaseResponseJson fail() {
return resultData(ResponseCode.ERROR.val(), ResponseCode.ERROR.msg());
}
public static BaseResponseJson fail(String code, String msg) {
return resultData(code, msg, null);
}
public static BaseResponseJson fail(String code, String msg, Object data) {
return resultData(code, msg, data);
}
private static BaseResponseJson resultData(String code, String msg, Object data) {
BaseResponseJson resultData = new BaseResponseJson();
resultData.setCode(code);
resultData.setMsg(msg);
resultData.setData(data);
return resultData;
}
private static BaseResponseJson resultData(String code, String msg) {
BaseResponseJson resultData = new BaseResponseJson();
resultData.setCode(code);
resultData.setMsg(msg);
return resultData;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
if (auth_code2SessionRequest == null || StringUtils.isEmpty(auth_code2SessionRequest.getJs_code())) {
throw new WxJsCodeEmptyException();
}
{"code":"10002","msg":"微信小程序登录时获取的 js_code为空","data":null}
这样就比较友好了,当然根据业务还可以继续封装。
页面更新:2024-03-13
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号