/java/

Spring Boot. Change/configure port.

2015-11-15 12:15:30

When we have bunch of small spring boot application with embedded server and we want to run them on our computer we have to change port. Default for all Spring Boot application is 8080.

Assume we have simple app like this:

pom.xml

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<project http:="" maven-4.0.0.xsd&quot;="" maven.apache.org="" xmlns='"http://maven.apache.org/POM/4.0.0"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsd="" xsi:schemalocation='"http://maven.apache.org/POM/4.0.0'>
<modelversion>4.0.0</modelversion>
<groupid>pl.btbw</groupid>
<artifactid>spring-boot-test</artifactid>
<version>1.0-SNAPSHOT</version>
<parent>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-parent</artifactid>
<version>1.2.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-maven-plugin</artifactid>
</plugin>
</plugins>
</build>
</project>

SampleRestController

package pl.btbw.ctrl;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class SampleRestController {

    @RequestMapping("/")
    @ResponseBody
    private Map

<string, object=""> home() {
        Map<string, object=""> response = new HashMap<string, object="">();
        response.put("title", "Lorem Ipsum");
        return response;
    }
}

Application.java

package pl.btbw;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
@EnableAutoConfiguration
public class Application {

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

To run this application we have to create jar file by executing command maven package

and then we can "deploy" our app by

java -Dserver.port=8090 -jar target/spring-boot-test-1.0-SNAPSHOT.jar

as you see here we set system property using command line option -Dserver.port=8090

by this we can run plenty of apps on the same machine

java -Dserver.port=8090 -jar target/spring-boot-users-1.0-SNAPSHOT.jar
java -Dserver.port=8091 -jar target/spring-boot-article-1.0-SNAPSHOT.jar
java -Dserver.port=8092 -jar target/spring-boot-configuration-1.0-SNAPSHOT.jar