Spring Boot Change PortThe Spring Boot framework provides the default embedded server (Tomcat) to run the Spring Boot application. It runs on port 8080. It is possible to change the port in Spring Boot. We can change the port in Spring Boot by using the following interfaces and properties files:
Using application.properties fileWe recommend you to use the application.properties file if you want to change the default port. Because it is an easy and faster way to overwrite default values. We use the server.port property to overwrite the default property. For example, if we want to change default port 8080 to 8082, specify the property in application.properties file. application.properties We can also set the port property to 0. It scans the random port for the application. It uses a new port whenever we restart our application. application.properties Using application.yml fileSimilarly, we can also change the default port by using a yml file. Use either application.properties or application.yml file, both the files works in the same manner. application.yml Using EmbeddedServletContainerCustomizer InterfaceIf you are using Spring Boot 1.x version, it provides an interface EmbeddedServletContainerCustomizer to change the default port. EmbeddedServletContainerCustomizer Interface By using the EmbeddedServletContainerCustomizer, we can customize auto-configured embedded servlet containers. All the beans of this type get a callback with the container factory before starting the container itself. Therefore, we can set the port, addresses, and even error pages. It is defined in the org.springframework.boot.context.embedded package. The interface contains a method called customize(). It allows us to customize and specify ConfigurableEmbeddedServletContainer. It parses a parameter called container that we want to customize. SyntaxConfigurableEmbeddedServletContainer Interface It is an interface that reflects the changes in the EmbeddedServletContainerFactory interface (factory interface used to create EmbeddedServletContainers). It is defined in the org.springframework.boot.context.embedded package. It contains a method to change the port called the setPort() method. setPort() method The setPort() method configures the port of embedded servlet container should listen on. When we do not specify the port, it uses the default port 8080. If we want to disable the auto-start feature of the embedded server, use the port -1. The port -1 represents that it will not listen to any port but start the web application context. The method parses a parameter port (the port to set) of type int. SyntaxIn the following example, we have created a class named ServerCustomizer and implements the EmbeddedServletContainerCustomizer Interface. We have overridden the customize() method and invoke the setPort() method that sets the port 8097. ServerCustomizer.java Using WebServerFactoryCustomizer InterfaceSpring Boot 2.x version provides WebServerFactoryCustomizer interface to change the default port. It defined in the package org.springframework.boot.web.server. It parses a parameter T of type web server factory. SyntaxThe interface contains a method called customize(). It allows us to customize web server factories. It parses a parameter called factory that we want to customize. All the beans of this type get a callback with the server factory before starting the container itself. Therefore, we can set the port, addresses, and even error pages. SyntaxWebServerFactory Interface It is a tagging interface for factories. It is defined in org.springframework.boot.web.server package. It creates a WebServer. ConfigurableWebServerFactory It is an interface that configures web server factory. It is defined in the package org.springframework.boot.web.server. It extends WebServerFactory and ErrorPageRegistory. It contains a method to change the port called the setPort() method. setPort() The setPort() method configures the port of embedded servlet container should listen on. When we do not specify the port, it uses the default port 8080. If we want to disable the auto-start feature of the embedded server, use the port -1. The port -1 represents that it will not listen to any port but start the web application context. The method parses a parameter port (the port to set) of type int. SyntaxNote: This setPort() method is of interface ConfigurableWebServerFactoryIn the following example, we have created a class named ServerCustomizer that implements the WebServerFactoryCustomizer Interface. We have overridden the customize() method and invoke the setPort() method that sets the port 9001. ServerCustomizer.java Using Command Line ParameterWe can also change the port in Spring Boot by using the command line parameter. We must follow the steps given below:
Next TopicSpring |