Languages
[Edit]
EN

Spring Boot 2 - change default application port from source code

9 points
Created by:
Burhan-Boyce
458

To change change default application port from source code with Spring Boot 2 it is necessary to create own bean that overrides default port configuration. This article shows how to do it.

Notes:

  • read this article to see different methods how to change default port with Spring Boot,
  • read this article to see the exact place where to keep bean configurations.

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

 

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.
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