[英]Spring boot web app not running on tomcat 9
My web App works fine on Eclipse Photon STS, java 8 and Spring Boot 2.02 with the embeded tomcat using endpoint: 我的Web应用程序在Eclipse Photon STS,java 8和Spring Boot 2.02上运行正常,使用端点嵌入了tomcat:
http://localhost:8081/DataViewer/tspsPatentSearch
But when I compile the code into DataViewer.war file (using mvn package) and run it on Tomcat 9 on Linux with endpoint: 但是当我将代码编译成DataViewer.war文件(使用mvn包)并在Linux上使用端点在Tomcat 9上运行时:
http://myserver.com:8081/DataViewer/tspsPatentSearch
I get the infamous: 我得到了臭名昭着的:
Whitelabel Error Page
There was an unexpected error (type=Not Found, status=404).
/DataViewer/tspsPatentSearch
My pom.xml file is: 我的pom.xml文件是:
`<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion>
<groupId>com.clarivate</groupId>
<artifactId>dataviewer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>dataviewer</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<start-class>com.clarivate.dataviewer.DvMain</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- DS may need to remove for tomcat installation -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Required to find ojdbc6, because Oracle don't make it available to maven-->
<repositories>
<repository>
<id>codelds</id>
<url>https://code.lds.org/nexus/content/groups/main-repo</url>
</repository>
</repositories>
<build>
<finalName>DataViewer</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.clarivate.dataviewer.DvMain</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<description>TSPS data viewer</description>
In application.properties I have: 在application.properties我有:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
server.servlet.path=/DataViewer
My main class is: 我的主要课程是:
package com.clarivate.dataviewer;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class DvMain extends SpringBootServletInitializer {
static Logger logger = LogManager.getRootLogger();
public static void main(String[] args) {
logger.debug("DS1A in main()");
SpringApplication.run(DvMain.class, args);
logger.info("DS1C finished.");
}
//@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DvMain.class);
}
}
My MainController.java has: 我的MainController.java有:
@GetMapping("/tspsPatentSearch")
public String tspsPatentSearch(Model model) {
model.addAttribute("tspsPatent", new TspsPatent());
return "tspsPatentSearch";
}
The war file unpacks fine and there are no errors. war文件解压很好,没有错误。 In catalina.out we have:
在catalina.out我们有:
2018-10-04 12:09:09.954 INFO 12950 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/tspsPatentSearch],methods=[POST]}" onto public java.lang.String com.clarivate.dataviewer.controller.MainController.tspsPatentSearch(com.clarivate.dataviewer.model.TspsPatent,org.springframework.ui.Model,org.springframework.validation.BindingResult)
and no errors. 并没有错误。 I've tried this ie my package structure is correct and this ie my jsp's are in the correct location (data_viewer\\src\\main\\webapp\\WEB-INF\\jsp) and i'm now running short of ideas.
我试过这个,即我的包结构是正确的, 这就是我的jsp是在正确的位置(data_viewer \\ src \\ main \\ webapp \\ WEB-INF \\ jsp),我现在缺乏想法。 Any help much appreciated
任何帮助非常感谢
Edit: If I copy tspsPatentSearch.jsp into the war file top directory then tomcat finds it. 编辑:如果我将tspsPatentSearch.jsp复制到war文件顶层目录中,那么tomcat会找到它。 So it looks like tomcat is ignoring:
所以看起来tomcat忽略了:
spring.mvc.view.prefix=/WEB-INF/jsp/
or not finding application.properties at all. 或者根本没有找到application.properties。
Add this to your application.properties
: 将其添加到您的
application.properties
:
server.servlet.contextPath=/
I've taken your sample code and, assuming you annotated your MainController
simply as @Controller
, put a deployment together. 我已经采用了您的示例代码,并假设您将
MainController
简单地注释为@Controller
,将部署放在一起。 I changed a few things around, but I believe this was the bit that did it. 我改变了一些东西,但我相信这是有点做的。 I haven't been able to find any references explaining why it might be required by Tomcat, but I intend to keep looking.
我找不到任何引用来解释为什么Tomcat可能需要它,但我打算继续寻找。 I'll update you if I find anything.
如果我找到任何东西,我会更新你的。
Edit: 编辑:
I noticed some duplicate logging in Spring 2.0.2 related to this issue: https://github.com/spring-projects/spring-boot/issues/13470 我注意到Spring 2.0.2中有一些与此问题相关的重复日志记录: https : //github.com/spring-projects/spring-boot/issues/13470
The problem appeared fixed in 2.0.4 therefore I upgraded. 问题似乎在2.0.4中得到修复,因此我进行了升级。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
Additionally, I removed the server.servlet.contextPath=/
entry and tada I still can reach the Hello World jsp I set up. 另外,我删除了
server.servlet.contextPath=/
entry和tada我仍然可以到达我设置的Hello World jsp。 If upgrading is possible for you, maybe you could try that before adding something to application.properties
which could be considered duplicated functionality. 如果您可以进行升级,也许您可以在向
application.properties
添加内容之前尝试这一点,这可能被认为是重复的功能。 At least I can promise you a better logging experience. 至少我可以向你保证更好的日志记录体验。
Edit #2: 编辑#2:
No smoking gun, so far, but these (from 2.0.4) could be related: 到目前为止,没有吸烟枪,但这些(从2.0.4开始)可能是相关的:
Provide a consistent way to discover the primary DispatcherServlet's path 提供一致的方法来发现主DispatcherServlet的路径
Dispatcher servlets with a custom servlet name are not found by the mappings endpoint 映射端点找不到具有自定义servlet名称的Dispatcher servlet
Nothing looked likely on a surface scan of 2.0.3. 2.0.3的表面扫描看起来没什么可能的。 I'm going to give it a rest for now and give you a chance to try some stuff.
我现在打算休息一下,让你有机会尝试一些东西。 Good luck!
祝好运!
Edit #3: 编辑#3:
I'm sorry to continue suggesting you switch environment stuff around, but one difference I noted between what I tested and what you're working with is that you seem to be using Tomcat-9.0.0.M20 where as I was testing with 9.0.12 . 我很遗憾继续建议你切换环境的东西,但我在测试和你正在使用的东西之间注意到的一个区别是你似乎在使用Tomcat-9.0.0.M20,因为我正在测试9.0 .12 。
Whether you want to upgrade or not, a few things to note and/or do: 无论您是否想要升级,都需要注意和/或做一些事情:
1) Update your question with what you've got now if it's different than before. 1)如果与以前不同,请用现在的内容更新您的问题。 Include the
server.servlet.contextPath=/
in your application.properties
just so anyone else looking can see what you've done. 在
application.properties
包含server.servlet.contextPath=/
只是让其他任何人都可以看到你做了什么。
2) The exclusion you have for spring-boot-starter-tomcat
under spring-boot-starter-web
doesn't appear to do anything - you can verify by comparing the output from running mvn dependency:tree
before and after removal. 2)
spring-boot-starter-web
下对spring-boot-starter-tomcat
的排除似乎没有做任何事情 - 你可以通过比较运行mvn dependency:tree
输出前后mvn dependency:tree
的输出来验证。
3) I'm not sure that your spring-web
dependency is needed either, as that's brought in by default under the spring-boot-starter
. 3)我不确定你的
spring-web
依赖性是否也需要,因为默认情况下它是在spring-boot-starter
下引入的。
4) Now to your output. 4)现在输出你的输出。 Spring Boot is coming up (note the banner) and your classes are being found and acted upon.
Spring Boot即将推出(请注意横幅),您的课程正在被发现并采取行动。
catalina.out.DEBUG
also in your DS.log starting ~ 08:35:38.162 catalina.out.DEBUG
也在你的DS.log中开始~08:35:38.162
2018-10-12 09:30:17.322 DEBUG 55745 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [/data/apps/tomcat/apache-tomcat-9.0.0.M20/webapps/DataViewer/WEB-INF/classes/com/clarivate/dataviewer/controller/MainController.class]
2018-10-12 09:30:17.328 DEBUG 55745 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [/data/apps/tomcat/apache-tomcat-9.0.0.M20/webapps/DataViewer/WEB-INF/classes/com/clarivate/dataviewer/database/ReadFromDb.class]
2018-10-12 09:30:17.356 DEBUG 55745 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [/data/apps/tomcat/apache-tomcat-9.0.0.M20/webapps/DataViewer/WEB-INF/classes/com/clarivate/dataviewer/service/FileFuncs.class]
2018-10-12 09:30:17.357 DEBUG 55745 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [/data/apps/tomcat/apache-tomcat-9.0.0.M20/webapps/DataViewer/WEB-INF/classes/com/clarivate/dataviewer/service/StringFuncs.class]
...
2018-10-12 09:30:19.417 INFO 55745 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/tspsPatentSearch],methods=[POST]}" onto public java.lang.String com.clarivate.dataviewer.controller.MainController.tspsPatentSearch(com.clarivate.dataviewer.model.TspsPatent,org.springframework.ui.Model,org.springframework.validation.BindingResult)
2018-10-12 09:30:19.417 INFO 55745 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/tspsPatentSearch],methods=[GET]}" onto public java.lang.String com.clarivate.dataviewer.controller.MainController.tspsPatentSearch(org.springframework.ui.Model)
...
2018-10-12 09:30:19.769 INFO 55745 --- [ main] com.clarivate.dataviewer.DvMain : Started DvMain in 3.125 seconds (JVM running for 5.845)
And I do indeed note the mapping to /error
that's returned for your request at 09:32:11. 我确实注意到在09:32:11为您的请求返回的映射
/error
。
I find this strange: 我觉得这很奇怪:
2018-10-12 09:32:11.758 DEBUG 55745 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/DataViewer/DataViewer/error]
And it's different in DS.log: 它在DS.log中有所不同:
2018-10-12 08:36:56.136 DEBUG 6992 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/DataViewer /error]
Specifically /DataViewer/DataViewer/error
- have you tried requesting http://localhost:8081/DataViewer/DataViewer/tspsPatentSearch 特别是
/DataViewer/DataViewer/error
- 您是否尝试过请求http:// localhost:8081 / DataViewer / DataViewer / tspsPatentSearch
In general this appears as though everything is coming up but there is a misconfiguration somewhere that isn't allowing a request to map to the handler. 一般情况下,这看起来好像一切都会出现,但某处的配置错误,不允许请求映射到处理程序。
To confirm. 确认。 Tomcat adds the war file name to the end point, so if we create DataViewerX.war and set :
Tomcat将war文件名添加到终点,因此如果我们创建DataViewerX.war并设置:
server.servlet.path=/DataViewer
then the end point when running on an external tomcat is: 然后在外部tomcat上运行时的结束点是:
myServer.com/DataViewerX/DataViewer/tspsPatentSearch
but when we run on Eclispe, using the source code, then the end point is: 但是当我们使用源代码运行Eclispe时,终点是:
http://localhost:8081/DataViewer/tspsPatentSearch
which is a bit annoying but not a major problem. 这有点烦人但不是主要问题。 A way round this is to call the war file ROOT.war, then tomcat ignores the war file name and the 2 end points are the same, but I have multiple war files in the webapps dir so this solution isn't acceptable for me.
绕过这个方法是调用war文件ROOT.war,然后tomcat忽略war文件名,并且2个端点是相同的,但是我在webapps目录中有多个war文件,所以这个解决方案对我来说是不可接受的。 If anyone know's a way to allow the 2 end points to be the same then please say so, but it's not that important.
如果有人知道允许两个终点相同的方法那么请说出来,但这并不重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.