HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Spring Boot / Spring boot change default port of embedded server

Spring boot change default port of embedded server

By default, Spring boot applications start with embedded tomcat server start at default port 8080. We can change default embedded server port to any other port, using any one of below technique.

TIP – To scan for a free port (using OS natives to prevent clashes) use server.port=0. Now spring boot will find any unassigned http port for us.

1. Change default server port from properties file

We can do lots of wonderful things by simply making few entries in application properties file in any spring boot application. Changing server port is one of them.

1.1. application.properties

server.port=9000

1.1. application.yml

server:
  port : 9000

2. Change the server port programatically

EmbeddedServletContainerCustomizer interface is used to customize embedded tomcat configuration. Any beans of this type will get a callback with the container factory before the container itself is started, so we can set the port, address, error pages etc.

2.1. Spring boot2 – WebServerFactoryCustomizer interface

Change default server port in spring boot2 applications by implementing ConfigurableWebServerFactory interface.

@Component
public class AppContainerCustomizer 
			implements WebServerFactoryCustomizer< ConfigurableWebServerFactory > {
  
    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        factory.setPort(9000);
    }
}

2.2. Spring boot 1.x – EmbeddedServletContainerCustomizer interface

Change default server port in spring boot 1.x applications by implementing EmbeddedServletContainerCustomizer interface.

@Component
public class AppContainerCustomizer implements EmbeddedServletContainerCustomizer {

	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {

		container.setPort(9000);
	}
}

3. Spring boot change default port from command line

If the application is built as uber jar, we may consider this option as well. In this technique, we will pass ‘server.port’ argument during application run command.

$ java -jar -Dserver.port=9000 spring-boot-demo.jar

Let me know if you know any other way to accomplish to change the spring boot embedded server default port.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Vinit Solanki

    March 25, 2019

    Is server.port is just related to embedded servers ??

    • Lokesh Gupta

      March 25, 2019

      yes

Comments are closed on this article!

Search Tutorials

Spring Boot 2 Tutorial

  • Spring Boot – Introduction
  • Spring Boot – Starter parent
  • Spring Boot – Starter templates
  • Spring Boot – Multi-module project
  • Spring Boot – Annotations
  • Spring Boot – Auto configuration
  • Spring Boot – AOP
  • Spring Boot – Logging
  • Spring Boot – DevTools
  • Spring Boot – WAR Packaging
  • Spring Boot – REST API
  • Spring Boot – CRUD
  • Spring Boot – OAuth2
  • Spring Boot – Testing
  • Spring Boot – RestTemplate
  • Spring Boot – Thymeleaf
  • Spring Boot – Hibernate
  • Spring Boot – DataSource
  • Spring Boot – Error Handling
  • Spring Boot – Caching
  • Spring Boot – Retry
  • Spring Boot – BasicAuth
  • Spring Boot – H2 Database
  • Spring Boot – Ehcache 3.x
  • Spring Boot – Gson
  • Spring Boot – RMI
  • Spring Boot – Send Email
  • Spring Boot – Interview Questions

Spring Boot Tutorial

  • Spring Boot – CommandLineRunner
  • Spring Boot – Configure Jetty
  • Spring Boot – Tomcat Default Port
  • Spring Boot – Context Root
  • Spring Boot – SSL [https]
  • Spring Boot – Get all loaded beans
  • Spring Boot – PropertyEditor
  • Spring Boot – @EnableScheduling
  • Spring Boot – Jersey
  • Spring Boot – SOAP Webservice
  • Spring Boot – SOAP Client
  • Spring Boot – JMSTemplate
  • Spring Boot – REST APIs
  • Spring Boot – JSP View
  • Spring Boot – Actuator endpoints
  • Spring Boot – Role Based Security
  • Spring Boot – RSS / ATOM Feed
  • Spring Boot – Ehcache 2.x

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces