Java Spring Boot 2 Maven项目在Intellij上运行,但不在Tomcat上运行

Java Spring Boot 2 Maven项目在Intellij上运行,但不在Tomcat上运行,java,spring,maven,spring-boot,tomcat,Java,Spring,Maven,Spring Boot,Tomcat,我一直在尝试解决这个问题,但没有取得多大成功,因此任何暗示解决方案的线索都将不胜感激。我根本不是专家,所以这可能是主要问题 我目前有一个SpringBoot后端,如果我从Intellij SpringBoot运行它,它可以完美地运行。但是,如果我mvn clean install创建一个war包来部署在Tomcat8.5中,那么我尝试的所有路由都会出现404错误 这是父目录的pom.xml <?xml version="1.0" encoding="UTF-8"?> <proj

我一直在尝试解决这个问题,但没有取得多大成功,因此任何暗示解决方案的线索都将不胜感激。我根本不是专家,所以这可能是主要问题

我目前有一个SpringBoot后端,如果我从Intellij SpringBoot运行它,它可以完美地运行。但是,如果我mvn clean install创建一个war包来部署在Tomcat8.5中,那么我尝试的所有路由都会出现404错误

这是父目录的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>eu.sacs</groupId>
<artifactId>sacs-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>${project.artifactId}</name>
<description>Parent</description>

<modules>
    <module>sacs-knowledge-extraction</module>
    <module>sacs-repository-oracle</module>
    <module>sacs-service</module>
    <module>sacs-webservice</module>
</modules>

<prerequisites>
    <maven>3</maven>
</prerequisites>

<properties>
    <framework.version>0.0.0</framework.version>
    <java.version>1.10</java.version>
    <repo.path></repo.path>
</properties>

<dependencyManagement>
    <dependencies>
        <!-- Module dependencies -->
        <dependency>
            <groupId>eu.sacs</groupId>
            <artifactId>sacs-webservice</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>eu.sacs</groupId>
            <artifactId>sacs-service</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>eu.sacs</groupId>
            <artifactId>sacs-repository-oracle</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>eu.sacs</groupId>
            <artifactId>sacs-knowledge-extraction</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!-- Spring dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.5.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- JSON -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.6</version>
        </dependency>
        <dependency>
            <groupId>javax.json</groupId>
            <artifactId>javax.json-api</artifactId>
            <version>1.1.2</version>
        </dependency>

        <!-- Apache Jena -->
        <dependency>
            <groupId>org.apache.jena</groupId>
            <artifactId>jena-core</artifactId>
            <version>3.2.0</version>
        </dependency>

        <!-- Apache POI -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>

        <!-- Database - Oracle -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>2.6.0</version>
        </dependency>


        <!-- Apache Commons DBCP -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.1.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <version>1.1.0</version>
            <scope>test</scope>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.11.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.11.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-cas</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>

        <!-- Excel -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>


<repositories>
    <repository>
        <id>sacs</id>
        <url>file://${repo.path}</url>
    </repository>
</repositories>

<pluginRepositories>

    <pluginRepository>
        <id>sacs</id>
        <url>file://${repo.path}</url>
    </pluginRepository>

</pluginRepositories>

<build>
    <!-- <finalName>${warname}</finalName> -->
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.0.5.RELEASE</version>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <outputDirectory>.</outputDirectory>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.10</source>
                <target>1.10</target>
            </configuration>
        </plugin>
    </plugins>
</build>

在日志中部署
sacs-webservice-1.0-SNAPSHOT.war时没有错误。默认情况下,WAR名称是部署后的上下文路径,因此在您的情况下,示例URL看起来像
http://localhost:8080/sacs-webservice-1.0-SNAPSHOT/index.html

从IntelliJ中运行时,WAR文件名不是上下文路径的一部分,因此示例URL看起来像
http://localhost:8080/index.html
。这将进一步取决于IntelliJ中的运行配置,但对于
mvn clean package spring boot:Run
命令通常是这样的


底线是,URL不同,因为您选择使用独立的Tomcat。

在日志中部署
sacs-webservice-1.0-SNAPSHOT.war时没有错误。默认情况下,WAR名称是部署后的上下文路径,因此在您的情况下,示例URL看起来像
http://localhost:8080/sacs-webservice-1.0-SNAPSHOT/index.html

从IntelliJ中运行时,WAR文件名不是上下文路径的一部分,因此示例URL看起来像
http://localhost:8080/index.html
。这将进一步取决于IntelliJ中的运行配置,但对于
mvn clean package spring boot:Run
命令通常是这样的


底线是,URL是不同的,因为您选择使用独立的Tomcat。

好吧,最后通过以下方法基本上解决了这个问题

基本上,我必须用
SpringBootServletInitializer
扩展我的主类,覆盖
configure(SpringApplicationBuilder应用程序)
,最后,可能也是最重要的,添加这个缺少的maven依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

org.springframework.boot
弹簧启动机tomcat
假如

然后一切都按它应该的方式运行。

好吧,最后通过以下方法基本上解决了这个问题

基本上,我必须用
SpringBootServletInitializer
扩展我的主类,覆盖
configure(SpringApplicationBuilder应用程序)
,最后,可能也是最重要的,添加这个缺少的maven依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

org.springframework.boot
弹簧启动机tomcat
假如

然后一切都正常工作。

我已经试过了
http://localhost:8080/sacs-webservice-1.0-快照/后端路由
。同样的问题(应用程序实际上没有index.html)。尝试将war重命名为更具可读性的名称,甚至通过
localhost:8080/manager
访问根访问点,以避免出现拼写错误或失败的情况。在Intellij中,我不是在运行tomcat,而是在运行SpringBoot配置,我相信它(但不是完全确定)会运行
mvn SpringBoot:run
@epsilonmajorquezero尝试使用
http://localhost:8080/manager/text/list
,你看到你的战争了吗?是的,这完全可以正常工作:
/sacs-webservice-1.0-SNAPSHOT:running:0:sacs-webservice-1.0-SNAPSHOT
如果我进入管理器界面并点击应用程序的loink,它会将我重定向到
本地主机:8080//sacs-webservice-1.0-SNAPSHOT
这是404,没有问题,那里没有
index.html
。但是,如果我在后端选择任何路由,比如说
localhost:8080//sacs-webservice-1.0-SNAPSHOT/admin/getUsers
,在INtellij中,它返回的是一个完全正常的Json,它只返回404,登录时没有错误
catalina.out
,我刚刚试过运行
mvn spring boot:run
,但它无法部署tomcat,因为它发现了几个错误(我现在正试图解决这些错误)。其中一些与我以前看到的一些奇怪的东西有关:如果我使用
jdgui
查看
war
文件,我会看到其中一个包是空的(当它不是空的时候在jdgui中显示null)。但是当Tomcat将
war
包中的类炸回来时,我没想到这会是个问题,特别是因为Tomcat在部署时没有显示任何错误。最后修复了spring boot:run,它现在运行平稳,但在Tomcat中的行为相同。我已经尝试了
http://localhost:8080/sacs-webservice-1.0-快照/后端路由
。同样的问题(应用程序实际上没有index.html)。尝试将war重命名为更具可读性的名称,甚至通过
localhost:8080/manager
访问根访问点,以避免出现拼写错误或失败的情况。在Intellij中,我不是在运行tomcat,而是在运行SpringBoot配置,我相信它(但不是完全确定)会运行
mvn SpringBoot:run
@epsilonmajorquezero尝试使用
http://localhost:8080/manager/text/list
,你看到你的战争了吗?是的,这完全可以正常工作:
/sacs-webservice-1.0-SNAPSHOT:running:0:sacs-webservice-1.0-SNAPSHOT
如果我进入管理器界面并点击应用程序的loink,它会将我重定向到
本地主机:8080//sacs-webservice-1.0-SNAPSHOT
这是404,没有问题,那里没有
index.html
。但是,如果我在后端使用任何路由,比如说
localhost:8080//sacs-webservice-1.0-SNAPSHOT/admin/getUsers
,在INtellij中,它返回的是一个非常正常的Json,它只返回404,并且没有错误登录
catalina.out
。如果有帮助,我只是尝试运行
mvn spring boot:run
,然后
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>