springboot通用异常处理
程序员文章站
2022-05-04 08:41:48
需要关注的点:异常捕获请求参数回放统一异常处理异常捕获对于控制层controller可以忽略部分异常处理请求参数回放因为是在异常发生时serveletrequest已经被拆包一次,所以需要考虑怎么把请求参数保留起来Servlet提供了Filter可以在请求到达时进行参数处理.自定义Fliterimport org.springframework.stereotype.Component;import javax.servlet.*;import javax.se...
需要关注的点:
- 异常捕获
- 请求参数回放
- 统一异常处理
异常捕获
对于控制层controller可以忽略部分异常处理
请求参数回放
因为是在异常发生时serveletrequest已经被拆包一次,所以需要考虑怎么把请求参数保留起来
Servlet提供了Filter可以在请求到达时进行参数处理.
自定义Fliter
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Component
@WebFilter(urlPatterns = "/*",filterName = "customFilter")
public class CustomFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
ServletRequest requestWrapper = null;
if(servletRequest instanceof HttpServletRequest) {
requestWrapper = new CustomRequestWrapper((HttpServletRequest) servletRequest);
}
if(requestWrapper == null) {
filterChain.doFilter(servletRequest, servletResponse);
} else {
filterChain.doFilter(requestWrapper, servletResponse);
}
}
@Override
public void destroy() {
}
}
自定义请求包装类
import com.alibaba.fastjson.JSON;
import org.apache.commons.io.IOUtils;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class CustomRequestWrapper extends HttpServletRequestWrapper {
private String body;
public CustomRequestWrapper(HttpServletRequest request) {
super(request);
try {
String requestBody = "";
if ("POST".equals(request.getMethod())){
if (request.getParameterMap() == null || request.getParameterMap().isEmpty()){
requestBody = IOUtils.toString(request.getReader());
setBody(requestBody);
}else{
requestBody = JSON.toJSONString(request.getParameterMap());
setBody(requestBody);
}
}else{
requestBody = JSON.toJSONString(request.getParameterMap());
setBody(requestBody);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public ServletInputStream getInputStream() {
//String requestBodyStr = getBody();
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(getBody().getBytes());
ServletInputStream servletInputStream = new ServletInputStream() {
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
@Override
public int read(){
return byteArrayInputStream.read();
}
};
return servletInputStream;
}
@Override
public BufferedReader getReader() {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
public String getBody() {
return this.body;
}
public void setBody(String body) {
this.body = body;
}
}
统一异常处理
spring提供了controlleradvice对controller层进行基于异常及非200状态码的切面监控
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
* controller 增强器
*/
@ControllerAdvice
public class MyControllerAdvice {
/**
* 全局异常捕捉处理
* @param ex
* @return
*/
@ResponseBody
@ExceptionHandler(value = Exception.class)
public Map errorHandler(Exception ex,WebRequest request) {
{
ServletWebRequest servletWebRequest = (ServletWebRequest) request;
if (servletWebRequest.getRequest().getClass().equals(CustomRequestWrapper.class)){
CustomRequestWrapper servletRequest = (CustomRequestWrapper) servletWebRequest.getRequest();
String requestURI = servletRequest.getRequestURI();
String remoteAddr = servletRequest.getRemoteAddr();
String method = servletRequest.getMethod();
String statusCode = status.value() + "";
String requestBody = servletRequest.getBody();
String errorMessage = e.getMessage();
DateTime now = new DateTime();
String timeLocal = now.toString("yyyy-MM-dd")+"T"+now.toString("HH:mm:ss")+"+08:00";
//.....此处省略千言万语
}
Map map = new HashMap();
map.put("code", 100);
map.put("msg", ex.getMessage());
return map;
}
}
本文地址:https://blog.csdn.net/*nggui10000/article/details/107700219
上一篇: RabbitMQ中生产者丢消息的情况
下一篇: 聊一聊“鸭子类型“