Search code examples
spring-bootspring-mvcspring-restcontrollerspring-autoconfiguration

Spring Customize whitelabel error - redirect


I am trying to customize /error by implementing Error controller in a class annonated with RestController.

The Spring Boot app includes autoconfiguration Library and does not explicitly set or use MVC.

@RequestMapping("/error")
public String handler(){
return "Error Occurred";
}

The above code works fine when the error status is 401 and 404.

@RequestMapping("/error")
public void handler(HttpServletResponse response)
{
response.sendRedirect("http://<url>/home");
return;// edit: Even adding this statement is just setting location but not redirecting.
}

This is setting location in response with redirect url but not redirecting.

Requirement is to map the /error to external UI page.

Now, when I use, redirectview or responsentity, then, the logic in handler only works when /error is invoked manually and 404 is not invoking /error. It just says This page is not found. Can someone tell me is this because of autoconfiguration Library or am I missing anything?


Solution

  • Issue resolved. This application has some custom libraries which uses Spring Security. Without access token, this error is occuring. I need to explore more of 401 error but for now, to handle errors am using class with @RestControllerAdvice and set property spring.mvc.throw-exception-if-no-handler-found to true and also, disable whitelabel.

    Redirection logic works when the Authorization is successful.