一共两个文件,一个处理全局异常,保存信息到日志,另外一个负责返回异常信息给接口,只要将其文件添加到项目中,无需再做其他配置即可
1. MyExceptionHandler.java 全局异常处理类
优先执行此类,这里不好抓取404、403等错误信息
getMaps()请参考获取request中传递过来的参数信息
getHeaders()请参考获取request中传递过来的header信息
代码如下:
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
/**
* <h5>描述:全局异常处理类</h5>
* 优先执行此异常处理类
*
*/
@ControllerAdvice
public class MyExceptionHandler {
private final Logger log = LoggerFactory.getLogger(getClass());
@ExceptionHandler(value =Exception.class)
public String exceptionHandler(HttpServletRequest request, Exception e){
Map<String, Object> paramsMap = getMaps(request);
Map<String, Object> headersMap = getHeaders(request);
String requestUri = request.getAttribute("org.springframework.web.servlet.HandlerMapping.lookupPath").toString();
log.error("请求[{}]发生[{}]异常\r\n参数[{}]\r\nheader[{}]", requestUri,e.getMessage(),paramsMap,headersMap, e);
// 返回错误信息,交给其他异常处理类处理
return e.getMessage();
}
// =================== private method ===================
/**
* <h5>功能:获取从request中传递过来的参数信息</h5>
*
* @return Map<String, Object>
*/
private Map<String, Object> getMaps(HttpServletRequest request){
Map<String, Object> paramMap = new HashMap<String, Object>();
Enumeration<String> enume = request.getParameterNames();
while (enume.hasMoreElements()) {
String key = (String) enume.nextElement();
String[] values = request.getParameterValues(key);
paramMap.put(key, values.length == 1 ? request.getParameter(key).trim() : values);
}
return paramMap;
}
/**
* <h5>功能: 获取从request中传递过来的header信息</h5>
*
* @return Map<String, Object>
*/
private Map<String, Object> getHeaders(HttpServletRequest request) {
Map<String, Object> headerMap = new HashMap<String, Object>();
Enumeration<?> er = request.getHeaderNames();//获取请求头的所有name值
String headerName;
while(er.hasMoreElements()){
headerName = er.nextElement().toString();
headerMap.put(headerName, request.getHeader(headerName));
}
return headerMap;
}
}
2. MyErrorController.java 全局错误信息处理类
后执行此类,此类可以有效抓取,404、403等信息,代码如下:
import javax.servlet.http.HttpServletRequest;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.jnxj.common.vo.MessageBean;
/**
* <h5>描述:全局错误信息处理</h5>
* 执行完异常处理类之后执行此类
*/
@RestController
public class MyErrorController implements ErrorController {
@Override
public String getErrorPath() {
return "/error";
}
@RequestMapping("/error")
public String handleError(HttpServletRequest request) {
String message;
// 获取statusCode:401,404,500
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
if (statusCode == 400) {
message = "语义有误,当前请求无法被服务器理解或请求参数有误";
} else if (statusCode == 401) {
message = "当前请求需要用户验证";
} else if (statusCode == 403) {
message = "权限不足";
} else if (statusCode == 404) {
message = "请求的资源不存在";
} else {
message = "系统不小心生病了,正在紧急修复中,请耐心等候...o(╥﹏╥)o";
}
MessageBean messageBean = new MessageBean();
messageBean.setCode(statusCode.toString());
messageBean.setMessage(message);
return JSONObject.toJSONString(messageBean);
}
}
原文地址:https://blog.51cto.com/1197822/2465881
时间: 2024-11-07 14:57:52