Spring boot exception handling, usually in two ways
One is to use @ ControllerAdvice
The other is to use ErrorController, the default error handling BasicErrorController of springboot, which is the implemented interface
The first one is called adding interceptor. It's not good. The second one is less error information. It's also bad. It misses jsp's built-in objects, so it's modified in the second way. Before modification, first look where spring boot put the error information.
1, private static final String ERROR_ATTRIBUTE = DefaultErrorAttributes.class.getName()
+ ".ERROR";
2,javax.servlet.error.exception
3,javax.servlet.error.message
The first and second places are Throwable and the third places are error messages
See in the DefaultErrorAttributes class
public Throwable getError(WebRequest webRequest) {
Throwable exception = getAttribute(webRequest, ERROR_ATTRIBUTE);
if (exception == null) {
exception = getAttribute(webRequest, "javax.servlet.error.exception");
}
return exception;
}
When I find this place, I can see. I haven't dealt with it alone. I still use DefaultErrorAttributes. I just beautify it when I output
package com.topnet.quick.controller; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.boot.autoconfigure.web.ErrorProperties; import org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeStacktrace; import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.request.ServletWebRequest; import org.springframework.web.context.request.WebRequest; import com.alibaba.fastjson.JSON; import com.topnet.quick.frame.Result; import com.topnet.quick.frame.exception.QuickException; /** * Unified error handling * * @author lpf */ @RestController public class HttpErrorController implements ErrorController { private final static String ERROR_PATH = "/error"; private final ErrorProperties errorProperties = new ErrorProperties(); @Override public String getErrorPath() { // TODO Auto-generated method stub return ERROR_PATH; } @RequestMapping(path = ERROR_PATH) public Result error(HttpServletRequest request, HttpServletResponse response, QuickException e) { DefaultErrorAttributes defaultErrorAttributes = new DefaultErrorAttributes(); WebRequest webRequest = new ServletWebRequest(request); Map<String, Object> d = defaultErrorAttributes.getErrorAttributes(webRequest, isIncludeStackTrace(request, MediaType.TEXT_HTML)); String message = ""; if (d.get("message") != null) { message = d.get("message").toString(); } else { message = JSON.toJSONString(d); } Result result = Result.innerError(message); return result; } protected boolean isIncludeStackTrace(HttpServletRequest request, MediaType produces) { IncludeStacktrace include = errorProperties.getIncludeStacktrace(); if (include == IncludeStacktrace.ALWAYS) { return true; } if (include == IncludeStacktrace.ON_TRACE_PARAM) { return getTraceParameter(request); } return false; } protected boolean getTraceParameter(HttpServletRequest request) { String parameter = request.getParameter("trace"); if (parameter == null) { return false; } return !"false".equalsIgnoreCase(parameter); } }
Figure