English 中文(简体)
Spring Boot Tutorial

Spring Boot Resources

Selected Reading

Spring Boot - Tomcat Deployment
  • 时间:2024-11-03

Spring Boot - Tomcat Deployment


Previous Page Next Page  

By using Spring Boot apppcation, we can create a war file to deploy into the web server. In this chapter, you are going to learn how to create a WAR file and deploy the Spring Boot apppcation in Tomcat web server.

Spring Boot Servlet Initiapzer

The traditional way of deployment is making the Spring Boot Apppcation @SpringBootApppcation class extend the SpringBootServletInitiapzer class. Spring Boot Servlet Initiapzer class file allows you to configure the apppcation when it is launched by using Servlet Container.

The code for Spring Boot Apppcation class file for JAR file deployment is given below −

package com.tutorialspoint.demo;

import org.springframework.boot.SpringApppcation;
import org.springframework.boot.autoconfigure.SpringBootApppcation;

@SpringBootApppcation
pubpc class DemoApppcation {
   pubpc static void main(String[] args) {
      SpringApppcation.run(DemoApppcation.class, args);
   }
}

We need to extend the class SpringBootServletInitiapzer to support WAR file deployment. The code of Spring Boot Apppcation class file is given below −

package com.tutorialspoint.demo;

import org.springframework.boot.SpringApppcation;
import org.springframework.boot.autoconfigure.SpringBootApppcation;
import org.springframework.boot.builder.SpringApppcationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitiapzer;

@SpringBootApppcation
pubpc class DemoApppcation  extends SpringBootServletInitiapzer {
   @Override
   protected SpringApppcationBuilder configure(SpringApppcationBuilder apppcation) {
      return apppcation.sources(DemoApppcation.class);
   }
   pubpc static void main(String[] args) {
      SpringApppcation.run(DemoApppcation.class, args);
   }
}

Setting Main Class

In Spring Boot, we need to mention the main class that should start in the build file. For this purpose, you can use the following pieces of code −

For Maven, add the start class in pom.xml properties as shown below −

<start-class>com.tutorialspoint.demo.DemoApppcation</start-class>

For Gradle, add the main class name in build.gradle as shown below −

mainClassName="com.tutorialspoint.demo.DemoApppcation"

Update packaging JAR into WAR

We have to update the packaging JAR into WAR using the following pieces of code −

For Maven, add the packaging as WAR in pom.xml as shown below −

<packaging>war</packaging>

For Gradle, add the apppcation plugin and war plugin in the build.gradle as shown below −

apply plugin: ‘war’
apply plugin: ‘apppcation’

Now, let us write a simple Rest Endpoint to return the string “Hello World from Tomcat”. To write a Rest Endpoint, we need to add the Spring Boot web starter dependency into our build file.

For Maven, add the Spring Boot starter dependency in pom.xml using the code as shown below −

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

For Gradle, add the Spring Boot starter dependency in build.gradle using the code as shown below −

dependencies {
   compile( org.springframework.boot:spring-boot-starter-web )
}

Now, write a simple Rest Endpoint in Spring Boot Apppcation class file using the code as shown below −

package com.tutorialspoint.demo;

import org.springframework.boot.SpringApppcation;
import org.springframework.boot.autoconfigure.SpringBootApppcation;
import org.springframework.boot.builder.SpringApppcationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitiapzer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApppcation
@RestController
pubpc class DemoApppcation  extends SpringBootServletInitiapzer {
   @Override
   protected SpringApppcationBuilder configure(SpringApppcationBuilder apppcation) {
      return apppcation.sources(DemoApppcation.class);
   }
   pubpc static void main(String[] args) {
      SpringApppcation.run(DemoApppcation.class, args);
   }
   
   @RequestMapping(value = "/")
   pubpc String hello() {
      return "Hello World from Tomcat";
   }
}

Packaging your Apppcation

Now, create a WAR file to deploy into the Tomcat server by using Maven and Gradle commands for packaging your apppcation as given below −

For Maven, use the command mvn package for packaging your apppcation. Then, the WAR file will be created and you can find it in the target directory as shown in the screenshots given below −

Maven MVN Package

Maven Packaging Apppcation Target Directory

For Gradle, use the command gradle clean build for packaging your apppcation. Then, your WAR file will be created and you can find it under build/pbs directory. Observe the screenshots given here for a better understanding −

Gradle Clean Build Command

Maven Packaging Apppcation Target Directory

Deploy into Tomcat

Now, run the Tomcat Server, and deploy the WAR file under the webapps directory. Observe the screenshots shown here for a better understanding −

Tomcat Web Apppcation Maneger

webApps Directory

After successful deployment, hit the URL in your web browser http://localhost:8080/demo-0.0.1-SNAPSHOT/ and observe that the output will look as shown in the screenshot given below −

Successful Deployment Screenshot

The full code for this purpose is given below.

pom.xml

<?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>com.tutorialspoint</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>war</packaging>
   <name>demo</name>
   <description>Demo project for Spring Boot</description>
   
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.8.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <start-class>com.tutorialspoint.demo.DemoApppcation</start-class>
   </properties>

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

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

build.gradle

buildscript {
   ext {
      springBootVersion =  1.5.8.RELEASE 
   }
   repositories {
      mavenCentral()
   }
dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}

apply plugin:  java 
apply plugin:  ecppse 
apply plugin:  org.springframework.boot 
apply plugin:  war 
apply plugin:  apppcation 

group =  com.tutorialspoint 
version =  0.0.1-SNAPSHOT 
sourceCompatibipty = 1.8
mainClassName = "com.tutorialspoint.demo.DemoApppcation"

repositories {
   mavenCentral()
}
dependencies {
   compile( org.springframework.boot:spring-boot-starter-web )
   testCompile( org.springframework.boot:spring-boot-starter-test )
}

The code for main Spring Boot apppcation class file is given below −

package com.tutorialspoint.demo;

import org.springframework.boot.SpringApppcation;
import org.springframework.boot.autoconfigure.SpringBootApppcation;
import org.springframework.boot.builder.SpringApppcationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitiapzer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApppcation
@RestController
pubpc class DemoApppcation  extends SpringBootServletInitiapzer {
   @Override
   protected SpringApppcationBuilder configure(SpringApppcationBuilder apppcation) {
      return apppcation.sources(DemoApppcation.class);
   }
   pubpc static void main(String[] args) {
      SpringApppcation.run(DemoApppcation.class, args);
   }
   
   @RequestMapping(value = "/")
   pubpc String hello() {
      return "Hello World from Tomcat";
   }
}
Advertisements