Reputation: 398
I'm currently using a Spring Boot application I'm tinkering around the the error page and the messages given to it. Currently I can change the HTTP Status Number and Message, but I'm not sure how to change the "Unknown reason" or Description without changing it to something besides 418. Is there a way to customize those as well, or am I stuck with the embedded code provide?
Current Code Tinkering
for(String serialNo : serialNoList) {
if(serialNo.length() < MIN_SERIALNO_SIZE ) {
response.sendError(401, "Serial Number Length Exceeded: " + serialNo);
}
if(serialNo.length() > MAX_SERIALNO_SIZE) {
response.sendError(403, "Serial Number Legth Too Short: " + serialNo);
}
}
Upvotes: 2
Views: 2932
Reputation: 2890
First, you need to disable whiteLabel error pages.
server.error.whitelabel.enabled=false
or
// adding this on your main class
@EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class})
Now, create a html page (error.html), which you want to display and place it in resources/templates
directory, it will be picked automatically.
To customize
, differently for each error you can implement ErrorController
.
@Controller
public class CustomErrorController implements ErrorController {
// override this error path to custom error path
@Override
public String getErrorPath() {
return "/custom-error";
}
@GetMapping("/custom-error")
public String customHandling(HttpServletRequest request){
// you can use request to get different error codes
// request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)
// you can return different `view` based on error codes.
// return 'error-404' or 'error-500' based on errors
}
}
Upvotes: 2