This article contains how to change Spring boot change port using application.properties as well as using EmbeddedServletContainerCustomizer

1. Spring Boot change port using application.properties

In a standalone application the main HTTP port defaults to 8080, but can be set with server.port (e.g. in application.properties or as a System property).

server.port=8441

2. Spring Boot change port using  EmbeddedServletContainerCustomizer

package com.javadeveloperzone;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;


/**
 * Created by JavaDeveloperZone on 19-07-2017.
 */

@SpringBootApplication
@ComponentScan
// Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootConfig.class, args);            // it wil start application
    }

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return (container -> {
            container.setPort(8012);           // your new port
        });
    }
}

Output:

2017-07-20 23:07:38.765 INFO 56804 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8012 (http)
2017-07-20 23:07:38.770 INFO 56804 --- [ main] com.javadeveloperzone.SpringBootConfig : Started SpringBootConfig in 4.03 seconds (JVM running for 4.574)

References:

Spring Boot Change Port Doc

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No
Tags: ,

Leave a Reply

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