In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.