3

I am writing a Rest service using spring mvc framework and maven. I am using tomcat server for now. My pom for the project is

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>
</dependencies>

<properties>
    <java.version>1.8</java.version>
</properties>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

    </plugins>

</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>
</project>

This project uses tomcat server and runs on port 8080 by default. can anyone help me understand from where it picks up this configuration and how to change the port on which tomcat runs.

My preliminary analysis tells me there is some configuration done in spring.boot plugin that I need to override in my pom. Can anyone help me in overriding tomcat default port and run it on some other port.

Mercurial
  • 25
  • 1
  • 1
  • 5
  • Possible duplicate of [Spring Boot - how to configure port](http://stackoverflow.com/questions/21083170/spring-boot-how-to-configure-port) – Monica Granbois Apr 25 '16 at 05:04

4 Answers4

8

27.3.4 Customizing embedded servlet containers says (in part)

Common servlet container settings can be configured using Spring Environment properties. Usually you would define the properties in your application.properties file.

Common server settings include:

  • Network settings: listen port for incoming HTTP requests (server.port), interface address to bind to server.address, etc.

So, create a src/main/resources/application.properties and add

 server.port=${port:8081}

(or whatever port you want).

Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I tried the adding server.port statement in application.properties. It didn't work. The application is still picking up default port address specified by spring boot. – Mercurial Apr 26 '16 at 04:46
  • 1
    It worked with statement server.port=${port:8081} in application.properties file – Mercurial Apr 26 '16 at 04:55
3

we can change in application.properties of spring boot application like this

server.port=${port:8080}

Sumeet Tiwari
  • 343
  • 2
  • 11
0
  • Go to( your Tomcat destination)
  • C:\Program Files\Apache Software Foundation\Tomcat 8.0\conf
  • Inside conf you will find server.xml.
  • You can change the connector port to something like this.

Connector port="8081" (port number that you wish)

Sree Hari
  • 24
  • 8
0

Make sure that you do Update your Maven Project as well when a new application.properties file is created at src/main/resources. Otherwise, sometimes the changes may not get reflect at your end.

Update Maven Project:
Right Click Your Maven Project -> Maven -> Update Project

Example port number setting in application.properties file:

server.port = 4444

Note:
Update Maven project may not be required each time you change the port.

Rohit Gaikwad
  • 3,677
  • 3
  • 17
  • 40