Spring Boot - How to Change Server Port - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Saturday, June 23, 2018

Spring Boot - How to Change Server Port

In spring boot, By default embedded server port is 8080. You can change the port using the server.port properties.

P.S Tested with spring boot 2.0.3.RELEASE

1. Using Properties and YAML file

Update the port of the embedded server using the properties file. Change default port 8080 to 8081.

server.port=8081

If you want to deploy your application on random port then you can configure the port like:

server.port=0

Update the port of server using the yaml file. change default port 8080 to 8081.

server:
  port: 8081

For random port use

server:
  port: 0

2.1. Environment-Specific Ports

We can have the different port on different environments. We can do using the spring profiles. We can create property file for each environment.

We can have application-dev.properties for development environment with content like below.

server.port=8081

In testing environment.

server.port=8082

In production environment.

server.port=8083

3. Programmatic Configuration

We can configure the port programmatically by either setting the specific property when starting the application or by customizing the embedded server configuration.

First, let’s see how to set the property in the main @SpringBootApplication class:

package com.walking.techie;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
public class HelloWorldApplication {

  public static void main(String[] args) {
    Map<String, Object> props = new HashMap<>();
    props.put("server.port", 8085);

    new SpringApplicationBuilder()
            .sources(HelloWorldApplication.class)
            .properties(props)
            .run(args);
  }
}

Next, to customize the server configuration, we have to implement the WebServerFactoryCustomizer interface:

Note: This below code will work to Spring Boot 2.X version.

package com.walking.techie.config;

import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;

@Component
public class ServerPortCustomizer
    implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {

  @Override
  public void customize(ConfigurableWebServerFactory factory) {
    factory.setPort(8086);
  }
}

For String Boot 1.X, we can similarly implement the EmbeddedServletContainerCustomizer interface.

package com.walking.techie.config;

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

@Component
public class ServerPortCustomizer implements EmbeddedServletContainerCustomizer {

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

4. Command Line

We can change the port of server from the terminal. When packaging and running our application as jar. We can set the server.port argument with java command.

java -jar HelloApplication.jar --server.port=8083

Or With the equivalent syntax:

java -jar  -Dserver.port=8083 HelloApplication.jar

1 comment :