In spring boot, By default context path is "/". You can change the context path using server.servlet.context-path properties. This will work for the Spring Boot 2.X version.
For Boot 1.x, the property is server.context-path.
P.S Tested with spring boot 2.0.3.RELEASE
1. Using Properties and YAML file
Change the context path using the properties file.
server.servlet.context-path=/walking-techie
Change the context path using the yaml file.
server: servlet: context-path: /walking-techie
2. Programmatic Configuration
We can change the context path programmatically by either setting the specific property when starting the application or by customizing the embedded server configuration.
First, let’s see how to set the property in the main @SpringBootApplication class:
package com.walking.techie; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import java.util.HashMap; import java.util.Map; @SpringBootApplication public class HelloWorldApplication { public static void main(String[] args) { Map<String, Object> props = new HashMap<>(); props.put("server.port", 8085); // props.put("server.context-path", "/walking-techie"); spring boot 1.X version props.put("server.servlet.context-path", "/walking-techie"); new SpringApplicationBuilder() .sources(HelloWorldApplication.class) .properties(props) .run(args); } }
Next, to customize the context path, we have to implement the WebServerFactoryCustomizer interface:
Note: This below code will work to Spring Boot 2.X version.
package com.walking.techie.config; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.stereotype.Component; @Component public class ServerContextPathCustomizer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { @Override public void customize(ConfigurableServletWebServerFactory factory) { factory.setPort(8086); factory.setContextPath("/walking-techie"); } }
For String Boot 1.X, we can similarly implement the EmbeddedServletContainerCustomizer interface.
package com.walking.techie.config; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.stereotype.Component; @Component public class ServerContextPathCustomizer implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(8087); container.setContextPath("/walking-techie"); } }
4. Command Line
We can change the context path from terminal. When packaging and running our application as jar. We can set the server.servlet.context-path argument with java command.
java -jar hello-world-1.0.jar --server.servlet.context-path=/walking-techie
Or With the equivalent syntax:
java -jar -Dserver.servlet.context-path=/walking-techie hello-world-1.0.jar
No comments :
Post a Comment