0

I have a number of generic error pages which are used by multiple applications other than the one I have control of. I would like to configure the Spring Boot error controller to do a redirect to one of these pages. Unfortunately it's not working.

Eg.

  @Controller  
  public class MyCustomErrorController implements ErrorController {

    @GetMapping(value = "/error")
    public String handleError(HttpServletRequest request) {
      Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);

      if (status != null) {
        Integer statusCode = Integer.valueOf(status.toString());

        if (statusCode == HttpStatus.NOT_FOUND.value()) {
          return "redirect:https://www.example.com/error-404";
        }
      }

      return "redirect:https://www.example.com/error-500";
    }

    @Override
    public String getErrorPath() {
      return "/error";
    }
  }

If for example I purposefully mistype a URL I can see the response has the Location header with the 404 URL I am expecting but the browser doesn't actually redirect. Any ideas if it's possible to do a redirect from within a custom ErrorController?

Could this be because I'm trying to test this from localhost, and Strict-Transport-Security is ignoring the response Location header value (which is on a FQDN)?

PDStat
  • 5,513
  • 10
  • 51
  • 86

3 Answers3

1

try this.

  @Controller  
  public class MyCustomErrorController implements ErrorController {

    @GetMapping(value = "/error")
    public String handleError(HttpServletRequest request) {
      Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);

      if (status != null) {
        Integer statusCode = Integer.valueOf(status.toString());

        if (statusCode == HttpStatus.NOT_FOUND.value()) {
          return "redirect:/error-404"; //remove your https://www.example.com
        }
      }

      return "redirect:/error-500";
    }

    @Override
    public String getErrorPath() {
      return "/error";
    }
  }

** EDIT **
change the url mapping and try again:
error-404 -> error/404
error-500 -> error/500

@Controller  
  public class MyCustomErrorController implements ErrorController {

    @GetMapping(value = "/error")
    public String handleError(HttpServletRequest request) {
      Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);

      if (status != null) {
        Integer statusCode = Integer.valueOf(status.toString());

        if (statusCode == HttpStatus.NOT_FOUND.value()) {
          return "redirect:/error/404"; //remove your https://www.example.com
        }
      }

      return "redirect:/error/500";
    }

    @Override
    public String getErrorPath() {
      return "/error";
    }
  }  

error/404

@GetMapping("/error/404")

error/500

@GetMapping("/error/500")
HolyMoly
  • 46
  • 4
0

How about adding HttpServletResponse to the method param and use it for redirection?

HttpServletResponse response;       
response.sendRedirect("https://www.example.com/error-404");

Reference HERE

ricktg
  • 66
  • 5
0

Add the following properties in application.proerties file

server.error.whitelabel.enabled=false
DEBENDRA DHINDA
  • 1,163
  • 5
  • 14