Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot error controller retrieve original request

By default Spring Boot maps /error to BasicErrorController. I want to log the exception along with the request that causes the exception. How can I get the original request in BasicErrorController or a new CustomErrorController. It seems that Spring Boot will make a new request to /error when an exception is thrown and the orginal request info is gone or no way to map the error with the original request. enter image description here

like image 381
dibugger Avatar asked Feb 20 '19 23:02

dibugger


People also ask

What is exception handling in Spring Boot REST API?

Exception Handling in Spring Boot REST API. When you develop a Spring Bool RESTful service, you as a programmer are responsible for handling exceptions in the service. For instance, by properly handling exceptions, you can stop the disruption of the normal flow of the application.

What are the default error handling responses for Spring Boot errors?

Similarly, when dealing with REST requests, Spring Boot automatically returns a default JSON response in case of errors. This contains the same information as the aforementioned Whitelabel HTML error page and looks as follows: As you can see, the default Spring Boot error handling responses for REST does not provide much information.

What is the @controlleradvice annotation in Spring Boot?

The @ControllerAdvice annotation was introduced in Spring 3.2 to make exception handling logic easier and entirely definable in one place. In fact, @ControllerAdvice allows you to address exception handling across the whole application.

What is defaulthandlerexceptionresolver in Spring Boot?

DefaultHandlerExceptionResolver This resolver was introduced in Spring 3.0, and it's enabled by default in the DispatcherServlet. It's used to resolve standard Spring exceptions to their corresponding HTTP Status Codes, namely Client error 4xx and Server error 5xx status codes.


2 Answers

Get it by:

String url = (String) request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI);
like image 162
meadlai Avatar answered Oct 19 '22 06:10

meadlai


To avoid any misleading information, Spring Boot DOES NOT make a new request to /error endpoint. Instead, it wraps the exception in the original request and forwards it to /error endpoint. The request will be processed by BasicErrorHandler if you don't provide a custom error handler.

In this case, if you are using an interceptor, the interceptor will be invoked twice - one for the original request and the other for the forwarded request.

To retrieve the original request information, please look into the forwarded request's attributes. Basically, you can get the error message from these attributes javax.servlet.error.message, javax.servlet.error.status_code, org.springframework.web.servlet.DispatcherServlet.EXCEPTION.

And these are some resources that are related to error handling in Spring Boot:

  • spring.io/blog/2013/11/01/exception-handling-in-spring-mvc
  • https://www.baeldung.com/exception-handling-for-rest-with-spring
  • https://www.baeldung.com/spring-boot-custom-error-page
like image 4
dibugger Avatar answered Oct 19 '22 06:10

dibugger