I need to override the spring boot default error page. I injected the WebServerFactoryCustomizer bean and specified the corresponding error page. This works correctly in the spring boot embedded tomcat, but it still jumps to spring boot's default error page when I deploy to external tomcat. Is it missing from my configuration? Thanks for any help :)
here is the codes:
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(part of)
<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>