Java Spring boot web应用程序未在tomcat 9上运行

Java Spring boot web应用程序未在tomcat 9上运行,java,maven,jsp,spring-boot,tomcat,Java,Maven,Jsp,Spring Boot,Tomcat,我的web应用程序在Eclipse Photon STS、java 8和Spring Boot 2.02上运行良好,嵌入了tomcat,使用端点: http://localhost:8081/DataViewer/tspsPatentSearch http://myserver.com:8081/DataViewer/tspsPatentSearch 但是当我将代码编译成DataViewer.war文件(使用mvn包)并在Linux上的Tomcat9上运行它时 使用端点: http://lo

我的web应用程序在Eclipse Photon STS、java 8和Spring Boot 2.02上运行良好,嵌入了tomcat,使用端点:

http://localhost:8081/DataViewer/tspsPatentSearch
http://myserver.com:8081/DataViewer/tspsPatentSearch
但是当我将代码编译成DataViewer.war文件(使用mvn包)并在Linux上的Tomcat9上运行它时 使用端点:

http://localhost:8081/DataViewer/tspsPatentSearch
http://myserver.com:8081/DataViewer/tspsPatentSearch
我得到了臭名昭著的:

Whitelabel Error Page
There was an unexpected error (type=Not Found, status=404).
/DataViewer/tspsPatentSearch
我的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>
我的主要课程是:

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具有:

@GetMapping("/tspsPatentSearch")
public String tspsPatentSearch(Model model) {
     model.addAttribute("tspsPatent", new TspsPatent());

     return "tspsPatentSearch";                 
}
war文件解压良好,没有错误。在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)
没有错误。我已经尝试过ie我的包结构是正确的,ie我的jsp在正确的位置(data\u viewer\src\main\webapp\WEB-INF\jsp) 现在我的想法越来越少了。非常感谢您的帮助

编辑:如果我将tspsPatentSearch.jsp复制到war文件的顶层目录中,tomcat就会找到它。看来tomcat忽略了:

spring.mvc.view.prefix=/WEB-INF/jsp/

或者根本找不到application.properties。

将其添加到您的
应用程序.properties

server.servlet.contextPath=/
我已经获取了您的示例代码,并且假设您将
MainController
注释为
@Controller
,将部署放在一起。我改变了一些事情,但我相信正是这一点起了作用。我还没有找到任何参考资料来解释为什么Tomcat可能需要它,但我打算继续寻找。如果我发现什么,我会告诉你的

编辑:

我注意到Spring 2.0.2中与此问题相关的一些重复日志记录:

该问题在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>
我确实注意到了在09:32:11为您的请求返回的到
/error
的映射

我觉得这很奇怪:

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]
而在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]
特别是
/DataViewer/DataViewer/error
-您是否尝试请求

一般来说,这看起来好像一切都在进行中,但是在某个地方有一个配置错误,不允许请求映射到处理程序。

确认。 Tomcat将war文件名添加到端点,因此如果我们创建DataViewerX.war并设置:

server.servlet.path=/DataViewer
那么在外部tomcat上运行时的终点是:

myServer.com/DataViewerX/DataViewer/tspsPatentSearch 
但是,当我们使用源代码在Eclispe上运行时,终点是:

http://localhost:8081/DataViewer/tspsPatentSearch
这有点烦人,但不是什么大问题。解决这个问题的方法是调用war文件ROOT.war,然后tomcat忽略war文件名,两个端点是相同的,但是我在webapps目录中有多个war文件,所以这个解决方案对我来说是不可接受的。
如果有人知道允许两个端点相同的方法,请说出来,但这并不重要。

你试过吗?试一下
http://localhost:8081/tspsPatentSearch
谢谢,我刚刚尝试了这两个端点,我得到了相同的404错误,你能发布完整的DvMain类,包括所有imports@kj007我已经更新了我原来的帖子谢谢你的建议,我正在研究它。我遇到的问题是,当我将SpringBootStarter父版本更改为2.0.4时,我在war文件中得到了两组spring文件,即2.0.2和2.0.4(但在构建库中只有2.0.4)。我试过Maven->Update项目和其他各种东西,但到目前为止没有成功。我修复了双spring文件,但现在(在Eclipse上),它在SpringBootStarter父级2.0.2上运行良好,在SpringBootStarter父级2.0.4上返回404。当我切换回2.0.2时,它再次工作(仅在eclipse上)@DS。我昨晚没有在Eclipse中做任何测试,所以我没有任何东西可以提供给您。我没有找到一个令人满意的方法来反思这个过程,看看在哪里映射了什么。如果有帮助并且您还没有这样做,您可以将
logging.level.root=DEBUG
添加到
application.properties
中,并获得大量的输出,其中可能包含有关JSP编译的信息(或者没有)。我对这种语法不是很肯定——我没有昨晚的测试床。我已经打开了调试。生成的eclipse日志文件位于以下位置:。“DS1C”之前的位是spring启动时,DS1C之后的位是尝试命中端点时。这个文件有150K行长!myserver.com:8081/DataViewer/DataViewer/tspsPatentSearch:),不知道为什么。谢谢。