In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to handle the exception of error interface in Spring Boot? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Why is there this blog?
This is the beginning of the story. In a lazy afternoon, a sore day, I was testing the interface, and then I saw the following picture, they are like this and like this:
This is obviously a hint when something goes wrong with the application. Unfortunately, the company's projects seem to have all kinds of strange error and exception messages recently, but the developers look confused because they can't locate the problem quickly. So I decided it was time to do a good job with this exception handling (a little bit of customization to make the information more detailed).
How does Spring Boot handle exception information?
It's true if someone says to handle it through @ ExceptionHandler, but what I'm talking about here is another way, that is, when an exception occurs, put the Request forward on an error-handling interface, which defaults to / error. Spring Boot provides a default BasicErrorController to handle requests for this / error interface. One method handles the response json format data, and the other function responds to a html page, the Whitelabel Error Page we saw earlier that can no longer fool things.
So when I see this code, I have another question, where is the definition of this attempt called error? At first I thought it was a jsp or html page inside the spring boot package, and then I started searching. I couldn't find it in the end. After looking at the source code by debug, I found this thing: ErrorMvcAutoConfiguration, there is a WhitelabelErrorViewConfiguration and a SpelView in it. This is it, a view called error, which we can obviously override as long as we declare another bean name as error.
Of course, I get a rough idea of how spring boot handles exceptions through the / error interface. But I still feel psychologically empty, and I still feel that something is missing. By the way, I said before that if there is an exception, I will put the current Request forward on the / error interface. By the way, I now want to know where the forward is performed.
How to forward a request to the / error interface
To be honest, I've been looking for the answer to this question for a while. In the end, I found out that the forward operation was not done in Spring. It must be stated in advance that my project uses embedded tomcat. As for other types of containers, this blog may not be applicable. The operation of forward to / error is done in the org.apache.catalina.core.StandardHostValve of tomcat.
This is the answer to the question on how to handle the exception of the error interface in Spring Boot. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.