Search code examples
springspring-bootspring-mvcspring-restcontroller

Starting Spring boot REST controller in two ports


Is there a way to have two rest controller running on two different ports from one spring boot application ?

Say for example Controller_A running in http://localhost:8080 and Controller_B running in http://localhost:9090 in one SpringBoot main Application ?


Solution

  • One way of doing this is actually creating two application properties;

    app-A.properties

    server.port=8080
    

    app-B.properties

    server.port=9090
    

    And then in your controllers, put annotation like below;

    @Profile("A")
    public class ControllerA {
       ...
    }
    
    @Profile("B")
    public class ControllerB {
       ...
    }
    

    Finally you need to launch your application twice with following settings;

    java -jar -Dspring.profiles.active=A awesomeSpringApp.jar
    java -jar -Dspring.profiles.active=B awesomeSpringApp.jar