2

During develepment (under Netbeans), if e.g. an internal server error appears in my java SpringBoot(2.1.3) project with jetty, I get the error page named 500.html that is located in the src/main/resources/templates/error/500.html :

enter image description here

Yet when deployed in production, the 500.html (same for 404.html) does not show because of

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/error/500], template might not exist or might not be accessible by any of the configured Template Resolvers

although the templates are located in the /BOOT-INF/classes/templates/error/ folder of the jar :

enter image description here

These templates are called in an error controller :

@Controller
public class MyErrorController implements ErrorController {

    @RequestMapping("/error")
    public String handleError(Model model, HttpServletRequest request) {
        Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);

        if (status != null) {
            Integer statusCode = Integer.valueOf(status.toString());

            model.addAttribute("errorCode",
                    statusCode);

            if (statusCode == HttpStatus.NOT_FOUND.value()) {
                return "/error/404";
            } else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
                return "/error/500";
            }

        }

        return "/error/error";
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }

My project pom.xml features the spring-boot-maven-plugin as defined below :

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.3.RELEASE</version>                
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

Please note : all other templates can be found.

I read this other SO question but my project tree seems correct.

What should I do to make SpringBoot find my error templates ?

Any help appreciated,

HelloWorld
  • 2,275
  • 18
  • 29

1 Answers1

0

In the controller above the path to the error pages is wrong. The path should not start with /. So instead of

 if (statusCode == HttpStatus.NOT_FOUND.value()) {
                return "/error/404";
            } else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
                return "/error/500";
            }

        }

        return "/error/error";

the controller should be (without the leading /) :

 if (statusCode == HttpStatus.NOT_FOUND.value()) {
                return "error/404";
            } else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
                return "error/500";
            }

        }

        return "error/error";

Please mind that still there should be one leading / in the getErrorPath() as shown in Baeldung's post :

@Override
    public String getErrorPath() {
        return "/error";
    }
HelloWorld
  • 2,275
  • 18
  • 29