【发布时间】:2019-10-25 10:43:06
【问题描述】:
在开发期间(在 Netbeans 下),例如一个内部服务器错误出现在我的 java SpringBoot(2.1.3) 项目中,我得到了一个名为 500.html 的错误页面,它位于 src/main/resources/templates/error/500.html 中:
然而,当部署在生产环境中时,500.html(404.html 相同)由于
而没有显示org.thymeleaf.exceptions.TemplateInputException:解析模板时出错 [/error/500],模板可能不存在或可能无法被任何已配置的模板解析器访问
虽然模板位于 jar 的 /BOOT-INF/classes/templates/error/ 文件夹中:
这些模板在错误控制器中被调用:
@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";
}
我的项目pom.xml 具有spring-boot-maven-plugin,定义如下:
<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>
请注意:可以找到所有其他模板。
我阅读了this other SO question,但我的项目树似乎是正确的。
我应该怎么做才能让 SpringBoot 找到我的错误模板?
任何帮助表示赞赏,
【问题讨论】:
标签: spring-boot spring-boot-maven-plugin