异常处理errorPage [errorCode = 0,location = / error]

时间:2019-03-22 16:55:32

标签: java spring spring-boot

我的Spring Boot项目曾经在Mac上运行良好。今天,我将代码拖到Windows膝上,然后出现了问题,当出现错误(例如404、500)时,显示的是Tomcat错误页面,而不是我的自定义错误页面。同时,后端报告了一个错误:

2019-03-22 23:55:18.659 ERROR 11000 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost]           :Exception Processing ErrorPage[errorCode=0, location=/error]**

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getHttpServletMapping()Ljavax/servlet/http/HttpServletMapping;

Spring Boot版本为2.1.13,使用依赖项为:

plugins {
    id 'org.springframework.boot' version '2.1.3.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'
apply plugin: 'project-report'

group = 'cn.itscloudy'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

这是一个简单的演示,依赖项仅包含网络和百里香。

error.html在路径resources/template下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Error</title>
</head>
<body>
<h1>Error Page</h1>
</body>
</html>

这是所有控制台启动日志

2019-03-23 13:15:49.510  INFO 6192 --- [           main] c.i.e.ErrorPageTestApplication           : Starting ErrorPageTestApplication on SkyLand with PID 6192 (F:\WorkSpace_intellij\error_page_test\out\production\classes started by chai in F:\WorkSpace_intellij\error_page_test)
2019-03-23 13:15:49.510  INFO 6192 --- [           main] c.i.e.ErrorPageTestApplication           : No active profile set, falling back to default profiles: default
2019-03-23 13:15:50.540  INFO 6192 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-03-23 13:15:50.560  INFO 6192 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-03-23 13:15:50.560  INFO 6192 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-03-23 13:15:50.560  INFO 6192 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\Java\jdk1.8.0_162\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Docker\Docker\Resources\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;D:\Java\jdk1.8.0_162\bin;D:\mysql-5.7.21\bin;F:\tomcat\bin;F:\gradle-4.4.1\bin;D:\Git\cmd;D:\Java\jdk1.8.0_162\jre\bin;F:\Node.js\;F:\Python\Scripts\;F:\Python\;D:\MATLAB\runtime\win64;D:\MATLAB\bin;D:\MATLAB\polyspace\bin;C:\WINDOWS\System32\OpenSSH\;D:\Microsoft VS Code\bin;F:\Redis;F:\Python\Scripts\django-admin.exe;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;F:\Python\Scripts\;F:\Python\;C:\Users\chai\AppData\Roaming\npm;D:\Microsoft VS Code\bin;C:\Users\chai\AppData\Local\Microsoft\WindowsApps;;.]
2019-03-23 13:15:50.670  INFO 6192 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-03-23 13:15:50.670  INFO 6192 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1080 ms
2019-03-23 13:15:50.859  INFO 6192 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-03-23 13:15:50.949  INFO 6192 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page template: index
2019-03-23 13:15:51.029  INFO 6192 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-03-23 13:15:51.029  INFO 6192 --- [           main] c.i.e.ErrorPageTestApplication           : Started ErrorPageTestApplication in 2.138 seconds (JVM running for 4.394)

如果我访问的页面不存在,它将跳转到传统的Tomcat 404页面,而不是error.html。后端将报告我在顶部显示的错误。

我在Google上搜索了很多,但还是不这样做。然后我尝试了1.5.9版本的Spring Boot,它起作用了!但是我不想更改Spring Boot的版本,因为我不得不更改大量代码

导致此问题的原因是什么?如何处理?

1 个答案:

答案 0 :(得分:0)

在我将IDEA更新到最新版本后,该问题已得到解决。