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

Most upvoted comments

@siller174 If you want to compile with -Werror you should suppress the deprecation warning using @SuppressWarnings.

We don’t call getErrorPath() anywhere, however implementing ErrorController is required at the moment to cause the auto-configuration of Boot’s BasicErrorController to 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 ErrorController marker 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.path property is now the only way to customize the actual error path.

If I remember correctly, we used to use ErrorController to 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 deprecated

getErrorPath() in org.springframework.boot.web.servlet.error.ErrorController has been deprecated

Compilation failure [ERROR] /.../.../CustomErrorController.java: warnings found and -Werror specified

version 2.3.0.RELEASE