How to start a spring boot tomcat server on a specified port through command prompt

Viewed 1824

I have a very simple "Hello World" kind of REST api created using Spring Boot that is accessible through http://localhost:8080/greeting/world without any problem.

I would like to start two more instances of this API on ports 8081 and 8082 but not able to do so. It says java.net.BindException: Address already in use: bind

Command Used:

mvn spring-boot:run -Dserver.port=8081

application.yml

spring:
  application:
    name: world-greeting-service

WorldGreetingServiceApplication.java

@RestController
@SpringBootApplication
public class WorldGreetingServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(WorldGreetingServiceApplication.class, args);
    }

    @RequestMapping("/greeting/world")
    public String greetWorld() {
        return "Hello World!";
    }

}

Am I missing anything?

2 Answers

Try using,

mvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8081'
java -jar world-greeting-service.jar --server.port=8081

This command is an equivalent of above answer and can be used when you don't have maven installed

Related