The quickest way to change the default port of a Spring Boot application is by overriding the server.port property value in configuration files.

By default, the embedded server launches the application on port 8080.

To change the default port to 8888, add the following line in application.properties:

server.port=8888

Now the embedded server will launch the application on port 8888. For YAML configuration files, add the following to application.yml:

server:
    port: 8888

Environment specific ports

If your application is deployed in different environments, you may want to run it on different ports.

For example, you may want to use port 8081 for development, and port 8081 for the production environment for your Spring Boot application.

Just add the following to application-dev.properties:

server.port=8081

Next, add the following line to application-prod.properties:

server.port=8081

Java configuration

To customize Spring Boot default port programmatically through Java, you can do the following:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.setDefaultProperties(Collections.singletonMap("server.port", "8888"));
        application.run(args);
    }
}

Command-line arguments

You could also use command-line arguments to specify the port for your Spring application:

$ java -jar -Dserver.port=8888 spring-boot-app.jar