How to customize exception response in springBoot
This article will explain in detail how to customize abnormal responses in springBoot. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
1 Custom exception returns json data,
/ / Don't forget to add @ ControllerAdvice
@ ControllerAdvice
Public class myExceptionHandler {
/ / both the browser and the client return json// @ ResponseBody
@ ExceptionHandler (UserNotExist.class)
Public Map handlerException (Exception e) {Map map = new HashMap (); map.put ("code", "user.notexist"); map.put ("message", e.getMessage ()); return map
}}
2 forward to / error for adaptive response @ ControllerAdvice public class myExceptionHandler {
/ both the browser and the client return json@ExceptionHandler (UserNotExist.class) public String handlerException (Exception e, HttpServletRequest request) {Map map = new HashMap (); request.setAttribute ("javax.servlet.error.status_code", 500); map.put ("code", "user.notexist"); map.put ("message", e.getMessage ()); request.setAttribute ("ext", map) Return "forward:/error";}
}
3 take customized errors out @ Component
Public class myErrorAtrributes extends DefaultErrorAttributes {
/ / WebRequest webRequest, note that the 1.x.x version is written differently from 2.1.0. Mine is 2.1.0public Map getErrorAttributes (WebRequest webRequest, boolean includeStackTrace) {Map errorAttributes = super.getErrorAttributes (webRequest,includeStackTrace); errorAttributes.put ("com", "maodong"); Map ext= (Map) webRequest.getAttribute ("ext", 0); errorAttributes.put ("ext", ext); return errorAttributes }
}
/ / Why do you write this: methods in the parent class DefaultErrorAttributes class
Public Map getErrorAttributes (WebRequest webRequest, boolean includeStackTrace) {
Map errorAttributes = new LinkedHashMap (); errorAttributes.put ("timestamp", new Date ()); this.addStatus (errorAttributes, webRequest); this.addErrorDetails (errorAttributes, webRequest, includeStackTrace); this.addPath (errorAttributes, webRequest); return errorAttributes;} / / Why can WebRequest webRequest get the same result as the lower version RequestAttrributes public interface WebRequest extends RequestAttributes {@ NullableString getHeader (String var1); @ NullableString [] getHeaderValues (String var1) Iterator getHeaderNames (); -} / / Why Map ext= (Map) webRequest.getAttribute ("ext", 0); can get extpublic interface RequestAttributes {int SCOPE_REQUEST = 0poliint SCOPE_SESSION = 1X string REFERENCE_REQUEST = "request"; String REFERENCE_SESSION = "session"; @ NullableObject getAttribute (String var1, int var2); void setAttribute (String var1, Object var2, int var3); void removeAttribute (String var1, int var2) So much for sharing about how to customize exception responses in springBoot. I hope the above content can be helpful to you and you can learn more. If you think the article is good, you can share it for more people to see.