Spring Boot – How to change Tomcat port

In Spring Boot, to change the embedded Tomcat initialized port (8080), update server.port properties.

P.S Tested with Spring Boot 1.4.2.RELEASE

1. Properties & Yaml

1.1 Update via a properties file.

/src/main/resources/application.properties

server.port=8888

1.2 Update via a yaml file.

/src/main/resources/application.yml

server:
  port: 8888

2. EmbeddedServletContainerCustomizer

Update via code, this overrides properties and yaml settings.

CustomContainer.java

package com.mkyong;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.stereotype.Component;

@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {

	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {

		container.setPort(8888);

	}

}

3. Command Line

Update the port by passing the system properties directly.

Terminal

java -jar -Dserver.port=8888 spring-boot-example-1.0.jar

References

  1. Spring Boot – Embedded servlet containers
  2. Spring Boot – Externalized Configuration
  3. Spring Boot – How to change context path

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
4 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
aanyanandpRohit Kumar SinghPriyo Recent comment authors
newest oldest most voted
Priyo
Guest
Priyo

Hi Myong,Your CustomContainer code worked..For a very long time I couldn’t get the solution of this..Thanks

aany
Guest
aany

Hello, My spring boot application is failing to start with Port In use exception. Even If I change with different port numbers then also i’m getting (Description:\r\n\r\nEmbedded servlet container failed to start. Port 8181 was already in use).
I’m not knowing what to do to start application

anandp
Guest
anandp

Hello, apart from these 3 options, can we change default Spring boot embedded Tomcat network, session and other configurations (whatever found under server.xml and catalina.properties standalone instance) like default values of server.ssl.client-auth, server.ssl.enabled, server.tomcat.protocol-header-https etc. directly under Maven repo .jar itself? This will ensure to harden insecure options at package level which multiple people can use. ServerProperties.class under package org.springframework.boot.autoconfigure.web defines these configurations but not sure exactly where these POJO’s called to set default values. Thanks

Rohit Kumar Singh
Guest
Rohit Kumar Singh

Hi Mkyong,

May you please tell me that how to enable https and http both ports at once in Spring Boot application.