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

If you know Spring Boot really simplifies the 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 by any reason if you want to change than 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 request goes to 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.


If you also need to change the port of embedded Tomcat in 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 in 2019 and coming years, not just to develop awesome application 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 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 random port i.e. 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.


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.


That's all about how to change the port of embedded tomcat server in Spring Boot application. As I said, by default 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 file, 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 in 2019 (courses)
5 Course to Master Spring Boot online in 2019 (courses)
10 Things Java Developer should learn in 2019 (goals)
10 Tools Java Developers use in their day-to-day life (tools)
10 Tips to become a better Java developer in 2019 (tips)
3 Best Practices Java Programmers can learn from Spring (best practices)
5 courses to learn Spring Boot and Spring Cloud in 2019( courses)
5 Spring Boot Annotations Java developer should learn (annotations)
5 Spring Books Java developers should read in 2019 (books)

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

No comments:

Post a Comment