Reputation: 1138
Spring boot: 2.2.2
IDE: Spring Tool Suite
One of the ways to create a custom error page is to create a controller class that implements ErrorController
. By overriding the getErrorPath()
method and returning /error
, I am able to return the expected custom error page.
But when I return another path, other than /error
, I get the following error:
This localhost page can’t be found. No webpage was found for the web address: http://localhost:8080/bruce
Why is that so?
@Controller
public class MyErrorController implements ErrorController{
// @RequestMapping("/error")
@RequestMapping("/error1")
public String handleError() {
return "customError";
}
@Override
public String getErrorPath() {
//return "/error";
return "/error1";
}
}
Upvotes: 1
Views: 1960
Reputation: 11456
So, it turns out that the output of the getErrorPath
is not used to redirect to the RequestMapping
of handleError
.
If you provide the following ErrorController
, on error it will still redirect to /error
when an error occurs:
@Controller
public class MyErrorController implements ErrorController {
@RequestMapping("/error")
public String handleError() {
return "customError";
}
@Override
public String getErrorPath() {
return "/some-non-existing-path";
}
}
I even found out that the following code works, and the RuntimeException
is never triggered:
@Controller
public class CustomErrorController implements ErrorController {
@RequestMapping("/error")
public String handleError() {
return "customError";
}
@Override
public String getErrorPath() {
throw new RuntimeException("This will not be called.");
}
}
Upvotes: 2