VOOZH about

URL: https://www.javacodegeeks.com/2018/03/how-to-change-the-default-port-of-spring-boot-application.html

⇱ How to change the default port of Spring Boot application - Java Code Geeks


By default, Spring Boot applications run on an embedded Tomcat via port 8080. In order to change the default port, you just need to modify server.port attribute which is automatically read at runtime by Spring Boot applications.

In this tutorial, we provide the common ways of modifying server.port attribute.

1- application.properties

Create application.properties file under src/main/resources and define server.port attribute inside it:

application.properties

server.port=9090

2- EmbeddedServletContainerCustomizer

You can customize the properties of the default servlet container through implementing the  EmbeddedServletContainerCustomizer interface as the following:

package com.programmer.gate;
 
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
 
public class CustomContainer implements EmbeddedServletContainerCustomizer {
 @Override
 public void customize(ConfigurableEmbeddedServletContainer container) {
 container.setPort(9090);
 }
}

The port defined inside the CustomContainer always overrides the value defined inside application.properties.

3- Command line

The third way is to set the port explicitly when starting up the application through the command line, you can do this in 2 different ways:

  • java -Dserver.port=9090 -jar executable.jar
  • java -jar executable.jar –server.port=9090

The port defined using this way overrides any other ports defined through other ways.

Published on Java Code Geeks with permission by Hussein Terek, partner at our JCG program. See the original article here: How to change the default port of Spring Boot application

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Hussein Terek
Hussein Terek
March 20th, 2018Last Updated: March 19th, 2018
1 90 1 minute read

Hussein Terek

Hussein is a senior software engineer with 5 years of experience in software design, development and integration. He is the author and founder of Programmer Gate (www.programmergate.com) blog.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
8 years ago

Great hint.

0
Reply
Back to top button
Close
wpDiscuz