spring-boot: Rework ErrorController now that getErrorPath() is unused and server.error.path must be used to configure the error path
Spring boot: 2.2.2
IDE: Spring Tool Suite
One of the ways to create a custom error page is to create a controller class that implements ErrorController. By overriding the getErrorPath() method and returning /error, I am able to return the expected my custom error page (located at resources/templates/customError.html).
But when I return another path, other than /error, I get the following error:
This localhost page can’t be found. No webpage was found for the web address: http://localhost:8080/bruce
Why is that so.
@Controller
public class MyErrorController implements ErrorController{
// @RequestMapping("/error")
@RequestMapping("/error1")
public String handleError() {
return "customError";
}
@Override
public String getErrorPath() {
//return "/error";
return "/error1";
}
}
Interestingly, I have done another experiment. The getErrorPath() returns /error1 , but I change the RequestMapping annotation to @RequestMapping("/error"), and that really return customError.html. This seems to imply that the path return by getErrorPath() is ignored.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 19 (11 by maintainers)
Commits related to this issue
- Deprecate unused ErrorController interface method This commit marks as deprecated an interface method that is no longer used, and changes all internal implementations to return `null` to make the fac... — committed to scottfrederick/spring-boot by scottfrederick 4 years ago
- Fix compile errors with spring boot 2.5 #609 - Former deprecated method ErrorController#getErrorPath was removed in Spring Boot 2.5. see https://github.com/spring-projects/spring-boot/issues/1984... — committed to mercedes-benz/sechub by de-jcup 3 years ago
- Update to Spring Boot 2.5.2 #609 - upgraded used Spring Boot version to 2.5.2 - upgraded gradle wrapper and gradle version to 6.9 (necessary for newer Spring Boot version) - fixed compile errors wi... — committed to mercedes-benz/sechub by de-jcup 3 years ago
- fix(dependency): Issue with kork-web while upgrading spring-boot to 2.5.14 While upgrading the spring-boot, the compilation of kork-web module failed with following error: ``` > Task :kork-web:compil... — committed to j-sandy/kork by j-sandy 2 years ago
- fix(dependency): Issue with kork-web while upgrading spring-boot to 2.5.14 While upgrading the spring-boot, the compilation of kork-web module failed with following error: ``` > Task :kork-web:compil... — committed to j-sandy/kork by j-sandy 2 years ago
- fix(dependency): Issue with kork-web while upgrading spring-boot to 2.5.14 While upgrading the spring-boot, the compilation of kork-web module failed with following error: ``` > Task :kork-web:compil... — committed to j-sandy/kork by j-sandy 2 years ago
- fix(dependency): Issue with kork-web while upgrading spring-boot to 2.5.14 While upgrading the spring-boot, the compilation of kork-web module failed with following error: ``` > Task :kork-web:compil... — committed to j-sandy/kork by j-sandy 2 years ago
- chore(dependencies): Upgrade Spring Boot to 2.5.14 (#1011) * chore(dependencies): Upgrade Spring Boot to 2.5.14 * fix(dependency): Issue with kork-jedis while upgrading spring-boot to 2.5.14 While... — committed to spinnaker/kork by j-sandy a year ago
@siller174 If you want to compile with
-Werroryou should suppress the deprecation warning using@SuppressWarnings.We don’t call
getErrorPath()anywhere, however implementingErrorControlleris required at the moment to cause the auto-configuration of Boot’sBasicErrorControllerto back off:https://github.com/spring-projects/spring-boot/blob/2725264be167617a194af51e46ec54e2f126f25b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.java#L106-L112
I’m not exactly sure what we should do here. An
ErrorControllermarker interface with no methods feels like a bit of a smell, but it does provide a way of identifying the user’s error controller and getting the auto-configured one to back off. I can’t really think of a better alternative.I have a feeling that the interface is unnecessary since Spring Boot 2.0, but I need to double check. I think that setting the
server.error.pathproperty is now the only way to customize the actual error path.If I remember correctly, we used to use
ErrorControllerto allow us to configure security, but we substantially simplified that in Spring Boot 2.0.Thanks, @brsolomon-deloitte. I’ve updated the release notes to list the deprecation.
@nkavian I’m not sure why the compiler that Maven’s using (typically
javac) wouldn’t respect the suppression of deprecation warnings. Until you’ve upgraded to Boot 2.5, you may also want to try marking your own implementation as@Deprecated.@nkavian Per the project policy, the method that was deprecated in 2.3.0 has been removed for the upcoming 2.5.0 release.
@scottfrederick After 1caca6e we can’t build projects with “-Werror” flag, because we must implement
getErrorPath(), but method is deprecatedgetErrorPath() in org.springframework.boot.web.servlet.error.ErrorController has been deprecatedCompilation failure [ERROR] /.../.../CustomErrorController.java: warnings found and -Werror specifiedversion 2.3.0.RELEASE