Languages
[Edit]
EN

Spring Boot - change default port 8080 to other port in embedded server

5 points
Created by:
Frank-van-Puffelen
379

1. Introduction

By default Spring Boot application uses embeded tomcat server that starts with default port 8080. Most commons approach to change default port to different is to change application.properties file configuration.

In this article different technics how to change port In Spring Boot 1 and 2 are described.

 

2. Properties and Yaml file examples

In this section same way to configure spring boot port is presented by with different file types.

2.1. application.properties file

This solution is dedicated if *.properties file is used to keep configuration. Add or change line server.port with own port number.

.../src/main/resources/application.properties file:

server.port=80

or

server.port: 80

Note: = and : separators are allowed.

Setting server.port in application.properties file - Spring Boot + IntelliJ
Setting server.port in application.properties file - Spring Boot + IntelliJ

2.2. application.yml file

This solution is dedicated if *.yml file is used to keep configuration. Add server and   port: 80 lines or change line   port: 80 with own port number.

.../src/main/resources/application.yml file:

server:
  port: 80

Note: do not forget to use indentations in right way. 

 

3. From source code level example

This approach overides settings located inside properties and yaml from source code level - can be used to override default values too.

Note: read this article to see the exact place where to keep bean configurations.

3.1. Spring Boot 1.x.x - custom container component class

In this approach EmbeddedServletContainerCustomizer class is used to create component that overrides configuration.

CustomContainer.java file:

package com.dirask.examples;

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

@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {

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

CustomContainer.java file location:

Setting server port in CustomContainer class - Spring Boot 1.x.x + IntelliJ
Setting server port in CustomContainer class - Spring Boot 1.x.x + IntelliJ

3.2. Spring Boot 2.x.x - web server factory bean

In this approach webServerFactory() method that returns ConfigurableServletWebServerFactory object is used.

ServerConfig.java file:

package com.dirask.examples;

import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ServerConfig {

    @Bean
    public ConfigurableServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();

        factory.addConnectorCustomizers(connector -> {
            connector.setPort(80);
        });

        return factory;
    }
}

ServerConfig.java file location:

Setting server port in ServerConfig class - Spring Boot 2.x.x + IntelliJ
Setting server port in ServerConfig class - Spring Boot 2.x.x + IntelliJ

 

3. Command line example

Run your application from command line adding port information.

$ java -jar -Dserver.port=80 com.dirask.examples-1.0.jar

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Spring Boot

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join