How to Deploy Spring-Boot Application on Tomcat server

Build and deploy scratch

MDA
4 min readApr 27, 2020

What We’ll Be Building

This tutorial will demonstrate how to deploy a spring boot application on tomcat server.

Requirements

  • Spring Boot 2.2.6
  • Apache Tomcat 9.0
  • Java 8
  • IntelliJ IDEA

Apache Tomcat is an open-source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and WebSocket technologies. Tomcat provides a “pure Java” HTTP web server environment in which Java code can run.
-Wikipedia

Let’s get started

As the first step, we have to create a spring boot project. Spring Initializer page which is one of the easiest ways to create spring boot project scratch.

  1. Visit https://start.spring.io web page
  2. Please Select bellow mentioned things
    Project: Maven Project
    Language: Java
    Spring Boot:2.2.6
    Enter the metadata your own
    Packaging: war
    Java:8 (in my case)
  3. As the dependencies add followings
    Spring Web
  4. after click generate. It will be downloaded project to your computer.

Then We’ll download Apache tomcat server

  1. To download tomcat, refer this link https://tomcat.apache.org/
  2. Select the Tomcat version on the left side menu. In my case, I’m going to use version 9. Fell free to use any version but above version 8 is recommended.
  3. Download Core zip in the binary distributions.
    (Extract zip that already downloaded)

Now we’ll try to run the created project on tomcat. For that, I use IntelliJ IDEA. (There are more ways to do this step)

  1. Import project that you’ve already created after extract.

2. Let’s see how to add tomcat server to IntelliJ. First, you need to add Tomcat and TomEE Integration(If you’ve already installed this plugin, please skip this step)
Preference > Plugins > Search (Tomcat and TomEE Integration) and select > Click Install JetBrains plugins button at the bottom > Install > Restart IntelliJ

3. Preference > Buil, Execution, Deployment > Application Server > Click + Icon > Select Tomcat EE Server > Give location’s path that you’ve downloaded tomcat binary in previous step.

4. Before going to the next step, you need to make sure tomcat starter dependency is an exit in the pom.xml and packaging type is war

5. Now, We’ll configure run time configuration. For that click the drop-down arrow on the top right corner of the tool menu. After clicking the arrow, edit configuration pop up will appear. Then click on that. Now you can see Run/Debug Configurations window box.
Then click + icon at the top left corner on the pop-up window.
Scroll down and select Tomcat Server > Local

6. In the server configuration window, you can give a name that you like for the server. In the server, tab selects JRE version. All other settings left as default.
Select the Deployment tab and click + icon in “deploy at server startup” > Select “Artifact” > Select name of your project: war (In my case example-tomcat: war) > Give name which you like for application context ( by this name you can access your application in tomcat server)

7. Before running, Let’s add a REST controller to make sure that whether the project is working fine.
Create a class named HomeResouce.java and add the following to it.

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

@RestController
public class HomeResource {

@GetMapping("/")
public String sayHello(){
return "Welcome to Tomcat Server :) ";
}

}

8. Now Let’s run (to run please click paly button right top corner )

If this error will have occurred please run this cmd “chmod a+x /path to your tomcat server/bin/catalina.sh”

If you have followed all steps perfectly, the screen below can be seen.

Thanks for reading!

--

--