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:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment