首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring 2 Maven项目在Intellij上运行,但在Tomcat上没有运行

Spring 2 Maven项目在Intellij上运行,但在Tomcat上没有运行
EN

Stack Overflow用户
提问于 2018-10-25 12:31:42
回答 2查看 976关注 0票数 0

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

我现在有一个SpringBoot后端,如果我从Intellij SpringBoot运行它,它就运行得很好。但是,如果我使用mvn干净安装来创建一个war包来部署在Tomcat8.5中,那么对于我尝试过的所有路由,我都会得到一个404错误

这是父目录的pom.xml

代码语言:javascript
复制
<?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的模块(其他模块被编译为webservice的jar )。

代码语言:javascript
复制
<?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日志的相关部分:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-26 13:00:56

最后,通过遵循文献资料,最终成功地解决了这个问题。

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

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

然后一切正常运转。

票数 0
EN

Stack Overflow用户

发布于 2018-10-25 12:38:14

在日志中部署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。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52989351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档