I am using Spring Boot to create a web app, and I am not sure how to change the URL from localhost:8080
to something like localhost:8080/myWebApp
.
I have a seen a lot of resources online referencing an application.properties
file and adding that to the classpath. But, I'm not sure exactly where to put that.
In my src/main/resources
?
How would I assign the URL within the file?
You need to set the property server.contextPath
to /myWebApp
.
Check out this part of the documentation
The easiest way to set that property would be in the properties file you are using (most likely application.properties
) but Spring Boot provides a whole lot of different way to set properties. Check out this part of the documentation
EDIT
As has been mentioned by @AbdullahKhan, as of Spring Boot 2.x the property has been deprecated and should be replaced with server.servlet.contextPath
as has been correctly mentioned in this answer.
As of spring boot 2 the server.contextPath
property is deprecated. Instead you should use server.servlet.contextPath
.
So in your application.properties file add:
server.servlet.contextPath=/myWebApp
For more details see: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#servlet-specific-server-properties
In your src/main/resources
put an application.properties
or application.yml
and put a server.contextPath
in there.
server.contextPath=/your/context/here
When starting your application the application will be available at http://localhost:8080/your/context/here
.
For a comprehensive list of properties to set see Appendix A. of the Spring Boot reference guide.
Instead of putting it in the application.properties you can also pass it as a system property when starting your application
java -jar yourapp.jar -Dserver.contextPath=/your/path/here
In Spring Boot 2 the property in eg application.properties
is server.servlet.context-path=/myWebApp
to set the context path.
The server.contextPath or server.context-path works if
in pom.xml
Add following dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Tomcat/TC server --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
In eclipse, right click on project --> Run as --> Spring Boot App.
The issue of changing the context path of a Spring application is handled very well in the post titled Spring Boot Change Context Path
Basically the post discusses multiple ways of realizing this viz.
Spring Boot , by default, serves content on the root context path (“/”). Sometimes we need a custom path. There are multiple ways to do it.
The most straightforward way of changing the context path is to set the property in the application.properties.
server.servlet.context-path=/myWebApp
Another way is to set the context path as a Java system property before even the context is initialized:
public static void main(String[] args) {
System.setProperty("server.servlet.context-path", "/myWebApp");
SpringApplication.run(Application.class, args);
}
You can also set the properties dynamically via command line arguments
java -jar app.jar --server.servlet.context-path=/myWebApp
Spring Boot can also depend on OS environment variables. On Unix based systems we can write:
export SERVER_SERVLET_CONTEXT_PATH=/myWebApp
On Windows, the command to set an environment variable is:
set SERVER_SERVLET_CONTEXT_PATH=/myWebApp
Note: The solutions will work for spring boot 2.xx . Hope it helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.