繁体   English   中英

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

[英]Spring Boot 2 Maven project running on Intellij but not on Tomcat

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

如果我从 Intellij SpringBoot 运行它,我目前有一个完美运行的 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>

而这个是webservice的那个(其他模块编译成jar给webservice)

<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>eu.sacs</groupId>
    <artifactId>sacs-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>sacs-webservice</artifactId>
<packaging>war</packaging>

<name>${project.artifactId}</name>
<description>Rest API and Controller</description>

<dependencies>
    <dependency>
        <groupId>eu.sacs</groupId>
        <artifactId>sacs-service</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc7</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-cas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
    </dependency>
</dependencies>

最后这里是Tomcat日志的相关部分:

25-Oct-2018 12:58:15.012 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [C:\apache-tomcat-8.5.34\webapps\sacs-webservice-1.0-SNAPSHOT.war]
25-Oct-2018 12:58:23.167 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
25-Oct-2018 12:58:23.636 WARNING [localhost-startStop-1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [216] milliseconds.
25-Oct-2018 12:58:23.677 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [C:\apache-tomcat-8.5.34\webapps\sacs-webservice-1.0-SNAPSHOT.war] has finished in [8,665] ms

在您的日志中部署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命令。

最重要的是,由于您选择使用独立的 Tomcat,因此 URL 是不同的。

好吧,终于通过遵循文档基本上设法弄清楚了。

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

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

然后一切正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM