How to Change the default port in Spring Boot

In previous post we have created spring boot example . Now we will learn how to change default port of embedded  server . Now we will going to learn  different  option how to change default port of server in spring boot .

1. Change Port using Properties Files : We can change spring boot application port by config server port in properties or yml file as below .

Set server.port property in application.properties file.

server.port = 8090

Set server port property in <application.yml file

server:
port: 8090

2. Change the spring boot application server port Programmatic

We can change server port programmatic by implements WebServerFactoryCustomizer  interface . We can set the port, address, error pages etc by  WebServerFactoryCustomizer interface .

package com.jp.helloWorld.config;

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;


@Component
public class CustomizationPort implements WebServerFactoryCustomizer
 < ConfigurableServletWebServerFactory > {

	public void customize(ConfigurableServletWebServerFactory server) {
		server.setPort(9001);
		}
	
}

3. Command Line Argument : We also have the option to set the port while starting our application. This is done by passing the argument through the command line.

java -jar -Dserver.port=9000 hello-spring-boot.jar

Leave a Reply

Your email address will not be published. Required fields are marked *

79 − = 70