Search code examples
javamavenspring-bootmaven-war-plugin

Springboot: application.properties path motification not taken into account


I'm creating a war file from a simple Spring boot (1.x) project, and I would like to modify the Context path.

For that purpose, I have an application.properties file that looks like this:

server.contextPath=/newpath

The project structure is the following:

.
src
    main
    ...
    resources
        application.properties

The pom.xml looks like this:

<?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>

When I perform a mvn package, I get a WAR file with the application.properties file located in /WEB-INF/classes, same content as the one I wrote. However, when deploying the war to Tomcat, I cannot access my API thru:

localhost:8080/newpath/example/some_controller

I can only query it via:

localhost:8080/example/some_controller

Am I missing something?


Solution

  • The server.context-path property only affects an embedded container. When deployed to an external container the context path is determined differently.

    In the case of Tomcat, you could copy your application to the webapps directory as a file named newpath.war. It should then be available at localhost:8080/newpath/example/some_controller.