Newsletter

Spring Boot – How to Change Default Tomcat Server Port

Spring-Boot-Tutorials » on Sep 3, 2017 { 5 Comments } By Sivateja

In our previous RESTful example, when we start the application Spring Boot’s inbuilt tomcat server by default will take 8080 as its port number, did you observe that 🙂 go back and have a look once. In this article, I am going to show you how to change that default tomcat’s port number 8080 to something else.

In Spring Boot, we can change tomcat’s port number in 2 ways…

  • Using application.properties
  • Using Java code change

Firstly, let me show you using application.properties. Consider the previous Creating a RESTful Web Service Example, here I just created application.properties file in src/main/resources no other changes.

Structure looks

application.properties

server.port = 2017

 

A single line server.port will change the Spring Boot tomcat’s port number, if you run the application the server will takes 2017 as its port number, you can check the port in the console and can execute the application.

Console

 

Using Java code change

In this approach, we will create a simple java class which implements EmbeddedServletContainerCustomizer interface of Spring Boot, this is a strategy interface for customizing auto-configured embedded servlet containers, and we need to override customize() method of that interface that’s it, let me show you an example.

Directory Structure

I have created a java class in com.java4s.app.server package with name Server.java

Server.java

package com.java4s.app.server;

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

@Component
public class Server implements EmbeddedServletContainerCustomizer {

	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) 
	{
		container.setPort(2018);
	}
}

If you run the application, now the server will consider 2018 as its port number.

Console

Note: If you use both application.properties and Java config, Spring Boot will give preference for Java only, I mean it takes 2018 as its tomcat port number, you can download this example and give a try 😉

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

5 Responses to “Spring Boot – How to Change Default Tomcat Server Port”
  1. Govind says:

    How to add application.properties file in Project

  2. spring boot learner says:

    Just on a side note. In spring boot 2.0 EmbeddedServletContainerCustomizer has been refactored to WebServerFactoryCustomizer.

  3. Chithra says:

    Hi sir, What is @component annotation?

  4. Naveen says:

    Hello Siva. I am Naveen.Regualr follower of your articles..Would you please explain Spring 2.X with Weblogic 12.1.X.I am trying to do since one month .I am getting failed to do it.WOuld you plaese add an article.Total my class is waiting for your reply.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.