【问题标题】:How to change Apache Tomcat default error page values?如何更改 Apache Tomcat 默认错误页面值?
【发布时间】:2020-11-06 21:12:53
【问题描述】:

我目前正在使用 Spring Boot 应用程序,我正在修改错误页面和提供给它的消息。目前我可以更改 HTTP 状态编号和消息,但我不确定如何更改“未知原因”或描述而不将其更改为 418 以外的其他内容。有没有办法自定义这些,或者我坚持嵌入代码提供?

当前的代码修补

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

【问题讨论】:

    标签: java spring-boot error-code


    【解决方案1】:

    首先,您需要禁用 whiteLabel 错误页面。

    server.error.whitelabel.enabled=false
    

    // adding this on your main class 
    @EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class})
    

    现在,创建一个您要显示的html页面(error.html)并将其放置在resources/templates目录中,它将被自动拾取。

    对于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
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-03
      • 2011-02-07
      • 2013-10-19
      • 1970-01-01
      • 2017-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      相关资源
      最近更新 更多