【问题标题】:Springboot: application.properties path motification not taken into accountSpring Boot:未考虑 application.properties 路径通知
【发布时间】:2018-04-09 21:53:12
【问题描述】:

我正在从一个简单的 Spring boot (1.x) 项目创建一个 war 文件,我想修改上下文路径。

为此,我有一个如下所示的 application.properties 文件:

server.contextPath=/newpath

项目结构如下:

.
src
    main
    ...
    resources
        application.properties

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>com.test.api</groupId>
    <artifactId>example</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Test project</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>1.5.9.RELEASE</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>example</finalName>
        <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        </plugins>
    </build>
</project>

当我执行 mvn 包 时,我得到一个 WAR 文件,其中 application.properties 文件位于 /WEB-INF/classes,内容与我编写的相同。但是,将战争部署到 Tomcat 时,我无法通过以下方式访问我的 API:

localhost:8080/newpath/example/some_controller

我只能通过以下方式查询:

localhost:8080/example/some_controller

我错过了什么吗?

【问题讨论】:

    标签: java maven spring-boot maven-war-plugin


    【解决方案1】:

    server.context-path 属性仅影响嵌入式容器。当部署到外部容器时,上下文路径的确定方式不同。

    对于 Tomcat,您可以将您的应用程序复制到 webapps 目录作为名为 newpath.war 的文件。然后它应该在 localhost:8080/newpath/example/some_controller 可用。

    【讨论】:

    • 关于这个的事情如下:我想要一个新的上下文路径,以便与部署在不同war文件中的其他应用程序共享同一个。这意味着我需要有两个同名的应用程序。或者,在 Tomcat 方面是否有其他选择可以实现这一目标?
    • 这是不可能的。部署到同一个 servlet 容器的每个应用程序必须具有不同的上下文路径。
    【解决方案2】:

    请确保您已将spring boot 可执行jar 项目转换为war 文件,转换为war 文件需要三个步骤。请按照此网址中给出的步骤进行操作 - https://www.mkyong.com/spring-boot/spring-boot-deploy-war-file-to-tomcat/

    【讨论】:

    • 我已经在项目中这样做了;但是,我认为这不是根本原因,因为我正确生成并部署了 war 文件。
    猜你喜欢
    • 2017-09-19
    • 2022-11-10
    • 2019-03-26
    • 1970-01-01
    • 2016-03-04
    • 2021-02-07
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    相关资源
    最近更新 更多