3 Ways to change Embedded Tomcat Server Port in Spring Boot Application

If you know Spring Boot really simplifies the Java web development by embedding essential libraries as well as a tomcat server to run Java web application. By default, this tomcat server listens on port 4588, and for any reason, if you want to change, then Spring boot provides several configuration options to achieve that. For example, I wanted tomcat to listen on port 8080 because port 80 is forwarded to 8080 in our Linux machine, and we wanted our clients to provide our client with a clean URL without a port.

Since by default, HTTP requests go to the port 80 and if you have forwarded that request to 8080 and if your Spring Boot application is listening on that port, then you can access it with port like http://localhost:8080/myspringbootapp or without port like http://localhost/myspringbootapp, both are Ok.

If you also need to change the port of embedded Tomcat in the Spring Boot application, then you have come to the right place. In this article, I'll show you three ways to make your spring boot application listen on a specified port.

Btw, if you are new to Spring Boot, then I also suggest you join a comprehensive course like Learn Spring Boot in 100 Steps on Udemy to learn in a more structured way. Spring boot is one of the frameworks you should learn this year and the coming years, not just to develop awesome applications but also to stay ahead with competition.



1. Changing Spring Boot Port in Tomcat using VM options

This is my favorite way to change the port of embedded tomcat in Spring boot application, just start your JVM with -Dserver.port=8080 or --server.port=8080 and it will listen on that port.

If you are running Spring boot application as an executable jar, then you can run your application using java -jar and provide the JVM arguments -Dserver.port=8080 as shown in the following example:

$ java -jar -Dserver.port=8080 myspringbootapp.jar

If you are using Eclipse to run the Spring boot application, then you can also go Run -> Run Configurations and then Arguments tab.

Here you can put the VM arguments -Dserver.port=8080 as shown below:

Spring Boot - 3 Ways to Change the port of Embedded Tomcat Server





2. Changing Spring Boot Port in Embedded Tomcat using Properties

Another way to change the port of embedded tomcat in the Spring Boot application is by specifying the server.port property in the resource file.

For example, if you want your Spring boot application to listen on port 8080, then you can specify server.port=8080 on the application.properties in /src/main/resources/ folder.

$ cat application.properties
server.port = 8080

If you want to use a random port like every time your Spring boot application starts, it uses a new port, you can set server.port=0. This is good if you don't care about port but not advised in any environment other than your local dev environment because it can cause port clashing.

Also, when you use the random port, you can get the port info by using the @Value annotation as shown below:

@Value("${local.server.port}")

If you already have a couple of properties for your Spring Boot application, then this is the preferred way to change the port of embedded tomcat server in Spring boot.

Btw, If you want to know more about @Value and other essential Spring annotations, I would recommend you to join a comprehensive Spring course like Spring Framework 5: Beginner to Guru. It's a great course to learn Spring in depth.

how to learn Spring Boot - best course




3. Changing Spring Boot Tomcat Port Programmatically

If you are a purist who likes to do everything in Java by writing code, then this method is for you. It allows you to specify the tomcat port for a Spring boot application in code:

@Configuration
public class ServletConfig {
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
   return (container -> {
     container.setPort(8080);
    });
  }
}

Though beware, if you need to change the port again, then you need to compile and rebuild the project. That's why it's better to put any setting on JVM argument or property file. If you use those methods, you can change the tomcat port of Spring boot application without recompiling and rebuilding them.

We have seen three different ways to change the Tomcat port in Spring Boot application and I must say this is a very convenient feature. Spring Boot itself is a convenient framework for Java developers, particularly if you are working with Web applications, REST API, and Microservices.

I recommend every Java developer to learn Spring Boot sooner than later. It will not only make your profile strong but also open the door for many job opportunities.

If you are working with Microservices and want to know how Spring Boot can help you, I suggest you join the Master Microservices with Spring Boot and Spring Cloud course by fellow blogger and instructor Ranga Rao Karnam on Udemy. It's a great course taught by a great teacher and Spring Boot expert.

best course to learn microservices with spring boot



That's all about how to change the port of embedded tomcat servers in the Spring Boot application. As I said, by default, the Spring boot application server listens on port 4588, but you can change the port by using these methods to make it listen on more familiar port 8080 or any other port of your choice. I generally prefer the VM option, but if you like properties files, feel free to use it. The programmatic way is very rare and only for purists.

Further Learning
Learn Spring Boot
Master Java Web Services with Spring Boot
Master Hibernate and JPA with Spring Boot in 100 Steps


Other Java and Spring articles you may like
  • 5 Spring Boot Features Every Java Developer Should Know (features)
  • Top 5 Free Courses to learn Spring and Spring Boot (courses)
  • 5 Course to Master Spring Boot in-depth (courses)
  • 10 Things Java Developer should learn  (goals)
  • Top 5 Courses to learn Microservices in Java (courses)
  • 10 Tools Java Developers use in their day-to-day life (tools)
  • 10 Advanced Spring Boot Courses for Java developers (courses)
  • 10 Tips to become a better Java developer (tips)
  • Top 5 Courses to Learn Microservices with Spring Cloud (courses)
  • 3 Best Practices Java Programmers can learn from Spring (best practices)
  • 5 courses to learn Spring Boot and Spring Cloud ( courses)
  • 5 Spring Boot Annotations Java developer should learn (annotations)
  • My favorite courses to learn Software architecture (courses)
  • 5 Spring Books Java developers should read (books)
  • Top 15 Spring Boot Interview Questions and Answers (questions)

Thanks for reading this article so far. If you find this Spring Boot tutorial and tips useful, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.

P. S. - If you are new to Spring Boot world and looking for free online training courses to learn Spring Boot yourself then I also suggest you to checkout Introducing Spring Boot (FREE Course) on Udemy. It's completely free and you just need a Udemy account to join this course.

2 comments:

  1. I had issues with the embedded spring-boot Tomcat instance overwriting my desired port of 8762 to 8080. To correct this, I found I needed to exclude the embedded version of tomcat from my build:

    * include the following tomcat.version property in my brac-job-engin pom.xml:

    ..
    9.0.24
    ...


    * move the server.port property from brac-job-engine's application.properties to bootstrap.properties

    * exclude the embedded Tomcat jar file from my dependencies:


    org.springframework.boot
    spring-boot-starter-web


    org.apache.tomcat.embed
    tomcat-embed-core




    * add in this version of the javax.servlet dependency:


    javax.servlet
    javax.servlet-api
    4.0.1

    ReplyDelete
    Replies
    1. Hi Annomouy,s can you post your comment again by escaping tags please?

      Delete

Feel free to comment, ask questions if you have any doubt.