简体   繁体   English

如何更改 Apache Tomcat 默认错误页面值?

[英]How to change Apache Tomcat default error page values?

I'm currently using a Spring Boot application I'm tinkering around the the error page and the messages given to it.我目前正在使用 Spring Boot 应用程序,我正在修改错误页面和提供给它的消息。 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?目前我可以更改 HTTP 状态编号和消息,但我不确定如何更改“未知原因”或描述而不将其更改为 418 以外的内容。是否也有自定义这些的方法,或者我坚持使用嵌入式代码提供?

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);
    }
}

错误页面

First, you need to disable whiteLabel error pages.首先,您需要禁用 whiteLabel 错误页面。

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.现在,创建一个 html 页面( error.html ),将其放置在resources/templates目录中,它会被自动选取。

To customize , differently for each error you can implement ErrorController .要对每个错误进行不同的customize ,您可以实施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
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM