Newsletter

How to Deploy Spring Boot Applications on External Tomcat Server

So far, in previous examples we used to deploy and run the applications using embedded tomcat server provided by the spring boot. Generally in the real-time projects we wont use inbuilt servers provided by the frameworks because of many reasons like security, maintenance and control. So in this article I will show you how can we deploy the spring boot applications on external servers (in this tutorial I am going to consider the external server as Tomcat).

Just do these changes to your spring boot application which you want to deploy it on to external tomcat server.

  • pom.xml, add dependency and packaging to war
  • Extend your main class with SpringBootServletInitializer and override its configure method
  • Generate WAR and deploy into the external server

If want you can change the context path also in the application.properties. (Optional)

pom.xml

<!-- to export as WAR -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
   <scope>provided</scope>
</dependency>

<!-- packaging to WAR -->
<packaging>war</packaging>

Final pom.xml

<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>com.java4s</groupId>
    <artifactId>SpringBootAppInExternalTomcat</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    
    <!-- packaging to WAR -->
    <packaging>war</packaging>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- Instruct spring boot not to use the inbuilt Tomcat server -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
    </properties>
</project>

Extend main Class with SpringBootServletInitializer

package com.java4s.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringBootApp extends SpringBootServletInitializer {

 @Override
 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  return application.sources(SpringBootApp.class);
 }

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

application.properties

server.contextPath=/springbootapp

SpringJava4sController.java

package com.java4s.app.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SpringJava4sController {

 @RequestMapping("/java4s-spring-boot-ex-tomcat")
 public String customerInformation() {
      return "Hey, I am from external tomcat";
 }

}

Generate a WAR file

Right click on pom.xml -> Run as -> Maven install, this will generate a WAR file in your target folder.  Just copy that WAR file into your tomcat’s webapps folder and start the server ( or ) you can deploy and test from your IDE (eclipse/sts) by importing that WAR into your work space and run that in the external tomcat [I tried importing into eclipse].

Output


Note:
If you observe the context path in the URL, its showing SpringBootAppInExternalTomcat-0.0.1-SNAPSHOT. Its not taking the context path we have given in the application.properties, rather its considering the context path as Artifact Id + Version from pom.xml. So if you want required context path, the only possibility is change pom.xml accordingly (if you know other ways please comment).

You can download the application and generate the WAR file with the above step and verify.

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
- Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

2 Responses to “How to Deploy Spring Boot Applications on External Tomcat Server”
  1. Ajith Alex says:

    My students are referring to this tutorials. Good article dude. 🙂

  2. Hussain says:

    Nice explanation bro.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2018 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.