在外部容器中部署时,Spring Boot自定义错误页面不起作用

时间:2018-06-21 02:22:54

标签: java spring spring-boot

我需要覆盖spring boot默认错误页面。我注入了WebServerFactoryCustomizer bean并指定了相应的错误页面。这在spring boot嵌入式tomcat中可以正常工作,但是当我部署到外部tomcat时,它仍然跳到spring boot的默认错误页面。 我的配置中缺少它吗?感谢您的帮助:)

这是代码:

  

ErrorConfig.java

 @Configuration
public class ErrorConfig {

    @Bean
    public WebServerFactoryCustomizer<TomcatServletWebServerFactory> sessionManagerCustomizer() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        return server -> {
            ErrorPage error400 = new ErrorPage(HttpStatus.BAD_REQUEST, "/error/400");
            ErrorPage error500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500");
            ErrorPage error404 = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404");
            ErrorPage error403 = new ErrorPage(HttpStatus.FORBIDDEN, "/error/403");
            ErrorPage error503 = new ErrorPage(HttpStatus.SERVICE_UNAVAILABLE, "/error/503");
            server.addErrorPages(error400, error500, error404, error403, error503);
        };
    }
}
  

ErrorController.java

@Controller
public class ErrorController {

    @RequestMapping(value = "/error/{code}")
    public String error(@PathVariable int code, Model model) {
        String page = "/error/error";
        switch (code) {
            case 403:
                model.addAttribute("code", 403);
                page = "/error/403";
                break;
            case 404:
                model.addAttribute("code", 404);
                page = "/error/404";
                break;
            case 500:
                model.addAttribute("code", 500);
                page = "/error/500";
                break;
            case 503:
                model.addAttribute("code", 503);
                page = "/error/503";
                break;
            default:
                model.addAttribute("code", 404);
                page = "/error/404";
        }
        return page;
    }

}
  

pom.xml(的一部分)

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

1 个答案:

答案 0 :(得分:0)

据我了解,TomcatServletWebServerFactoryWebServerFactoryCustomizer都不能在外部tomcat容器中工作,特别是在提供spring-boot-starter-tomcat范围的情况下。我认为您可以使用@ExceptionHandler批注和ErrorController界面将请求重定向到所需的页面(例如404/500等)。例如

@RestController
@ControllerAdvice
public class ExceptionController implements ErrorController {
    private static final String ERROR_PATH = "/error";

    @RequestMapping(value = "/error/{code}")
    public String handleError(@PathVariable int code) {
        String rtn = null;
        switch (code) {
            case 404:
                rtn = "Page not found";
                break;
            case 500:
                rtn = "Internal server error";
                break;
            default:
                rtn = "error";
                break;
        }
        return rtn;
    }

    @RequestMapping(value = ERROR_PATH)
    public void handleError(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect("error/404");
    }

    /**
     * implement ErrorController to handle 404
     */
    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }

    @ExceptionHandler(Exception.class)
    public void handleException(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException {
        response.sendRedirect("error/500");
    }
}