Spring Boot Change Context Path and Server Port Number
- Details
- Written by Nam Ha Minh
- Last Updated on 31 December 2023   |   Print Email
By default, a Spring Boot application is running with embedded server like Tomcat, which listens on port number 8080 and serves requests at empty context path - so developers can test the application on localhost with http://localhost:8080, as you can see in the logging message:
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path ''
You can change these defaults by putting the following entries in the application configuration file (application.properties):
- server.port: specifies the port number on which the server listens (default is 8080)
- server.servlet.context-path: specifies the context path of the application (default is empty)
For example, add the following properties in the application.properties file:
server.port=80 server.servlet.context-path=/MyApp
Then the application will be accessible on localhost via http://localhost/MyApp.
In case you’re using application.yml, the configuration should be like this:
server: port: 80 servlet: context-path: /MyApp
Restart the application for the change take effect, then you’ll see:
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 80 (http) with context path '/MyApp'
You can also change server port number and context path via the system variables passed to the java command. For example:
java -Dserver.port=80 -Dserver.servlet.context-path=/MyApp -jar MyApp.jar
This overrides the values of the corresponding properties in the Spring application configuration file. It’s useful when you run your Spring application in command line or do configuration in production environment (Docker, Heroku, AWS…).
Spring Tutorials:
Other Spring Boot Tutorials:
- Spring Boot Hello World Example
- Spring Boot Form Handling Tutorial with Spring Form Tags and JSP
- Spring Boot Hello World RESTful Web Services Tutorial
- How to create a Spring Boot Web Application (Spring MVC with JSP/ThymeLeaf)
- Spring Boot - Spring Data JPA - MySQL Example
- Spring Boot CRUD Example with Spring MVC – Spring Data JPA – ThymeLeaf - Hibernate - MySQL
- How to use JDBC with Spring Boot
- Spring Boot CRUD Web Application with JDBC - Thymeleaf - Oracle
- Spring Boot RESTful CRUD API Examples with MySQL database
- How to package Spring Boot application to JAR and WAR
Comments