Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize global exception capture in HandlerExceptionResolver

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to achieve global exception capture in HandlerExceptionResolver. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

There are undeniable exceptions in the project, and these exceptions are not caught. Frequent bug such as null pointer exception and so on. In the previous project, if we did not make any configuration, the container would automatically print error messages, such as tomcat 404 pages, 400 pages, etc. If we do the following configuration in web.xml, we will intercept the error and jump to the specified error page.

500 / 500.jsp

But this has lagged behind, and now we catch all exceptions by implementing spring's HandlerExceptionResolver interface. The main function is, for example, when we provide services in Web API, API itself fails, then we can set up an anomaly detection of the springboot framework itself before the API failure.

Demo is as follows:

Package com.demo.interceptor;import com.alibaba.fastjson.JSONObject;import com.credithc.kg.fetures.model.ResultDTO;import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Component;import org.springframework.web.servlet.HandlerExceptionResolver;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@Component@Slf4jpublic class MyHandlerExceptionResolver implements HandlerExceptionResolver {private ModelAndView modelAndView = new ModelAndView () @ Override public ModelAndView resolveException (HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {try {log.error ("system runtime exception: {}", e); httpServletResponse.setCharacterEncoding ("UTF-8"); httpServletResponse.setContentType ("application/json"); httpServletResponse.getWriter () .print (ResultDTO.fail ("system exception")) } catch (IOException E1) {log.error ("response IO exception: {}", E1);} return modelAndView;}} import lombok.Data;@Datapublic class ResultDTO {private Integer code; private String msg; private T body; private ResultDTO (Integer code, String msg, T body) {this.code = code; this.msg = msg; this.body = body } public static ResultDTO success (T body) {return success (200, "success", body);} public static ResultDTO success (Integer code, String msg) {return success (code,msg,null);} public static ResultDTO fail (String msg) {return success (500 msg);} public static ResultDTO success (Integer code, String msg, T body) {return new ResultDTO (code,msg,body) }} the above is how to achieve global exception capture in HandlerExceptionResolver. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report