Add context path to Spring Boot application
Why are you trying to roll your own solution. Spring-boot already supports that.
If you don't already have one, add an application.properties
file to src\main\resources
. In that properties file, add 2 properties:
server.contextPath=/mainstay
server.port=12378
UPDATE (Spring Boot 2.0)
As of Spring Boot 2.0 (due to the support of both Spring MVC and Spring WebFlux) the contextPath
has been changed to the following:
server.servlet.context-path=/mainstay
You can then remove your configuration for the custom servlet container. If you need to do some post processing on the container you can add a EmbeddedServletContainerCustomizer
implementation to your configuration (for instance to add the error pages).
Basically the properties inside the application.properties
serve as a default you can always override them by using another application.properties
next to the artifact you deliver or by adding JVM parameters (-Dserver.port=6666
).
See also The Reference Guide especially the properties section.
The class ServerProperties
implements the EmbeddedServletContainerCustomizer
. The default for contextPath
is ""
. In your code sample you are setting the contextPath
directly on the TomcatEmbeddedServletContainerFactory
. Next the ServerProperties
instance will process this instance and reset it from your path to ""
. (This line does a null
check but as the default is ""
it always fail and set the context to ""
and thus overriding yours).
How can I set context path in spring boot jboss/wildfly?
All of the server.* properties that Spring Boot supports only apply to the configuration of the embedded servlet container (Tomcat).
Jboss wildfly
Add your jboss-web.xml
file in this directory : /src/main/webapp/WEB-INF
Content:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<context-root>/</context-root>
</jboss-web>
Related Topics
Java 8's Streams: Why Parallel Stream Is Slower
How Does the Jvm Decided to Jit-Compile a Method (Categorize a Method as "Hot")
Invoking a Static Method Using Reflection
Add Bean Programmatically to Spring Web App Context
Why Doesn't Java Allow to Throw a Checked Exception from Static Initialization Block
When Would You Call Java's Thread.Run() Instead of Thread.Start()
Reliable File.Renameto() Alternative on Windows
Log4J Redirect Stdout to Dailyrollingfileappender
How to Create Table Using Ascii in a Console
Java "User.Dir" Property - What Exactly Does It Mean
Spring Boot - Inject Map from Application.Yml
Java Equivalent to C# Extension Methods
What's the Difference Between <> and <? Extends Object> in Java Generics
How to Sort an Array of Ints Using a Custom Comparator
How to Watch for Single File Change with Watchservice (Not the Whole Directory)