English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Deployment di Tomcat di SpringBoot

In questa sezione, impareremo come distribuire un'applicazione Spring Boot sul server Tomcat.

Include tre passaggi:

Imposta l'applicazione Spring Boot 创建Spring Boot WAR Distribuisci il WAR su Tomcat

Esempio

Creiamo un esempio Maven che possa essere distribuito su Tomcat

Imposta l'applicazione Spring Boot

Passo 1: Apri Spring Initializr http://start.spring.io .

Passo 2: 提供 Group Nome. Forniamo com.w3codebox.

步骤3: 提供 工件 ID。我们提供了 spring-boot-war-deployment-example。

步骤4: 添加 Spring Web 依赖项

<scope>provided</scope>
<plugin>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>

步骤5: 单击 Generate (生成)按钮。它包装了与项目相关的所有规范,并在我们的本地系统中下载了 jar 文件。

步骤6: 提取 jar文件。

步骤7: 导入,请按照以下步骤操作:

文件->导入->现有Maven项目->下一步->浏览->选择项目文件夹->完成

导入项目后,我们可以在IDE的 Package Explorer 部分中看到以下目录结构。

步骤8: 在包 com.w3codebox 中创建Controller类。我们创建了一个名为 DemoRestController的类。

在控制器类内部,我们定义了一个返回字符串的方法 hello().

DemoRestController.java

package com.w3codebox;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoRestController 
{
@GetMapping("/hello")
public String hello() 
{
return "Hello User, have a nice day.";
}
}

步骤9: 作为Java应用程序运行 SpringBootWarDeploymentExampleApplication.java 文件。

步骤10: 打开浏览器并调用URL http://localhost:8080/hello。

注意: 在进行下一步之前,请确保应用程序运行正常。

创建Spring Boot WAR

它利用了Spring Framework的Servlet 3.0支持,并允许我们在Servlet容器启动时配置应用程序。要创建用于部署的WAR,有 三个步骤:

在主类中扩展 SpringBootServletInitializer 类。 将嵌入式servlet容器标记为已提供. 将包装 JAR 更新为

让我们在应用程序中实现上述三个步骤。

步骤11: 打开 SpringBootWarDeploymentExampleApplication.java 初始化Tomcat所需的Servlet上下文。为实现相同目的,扩展了 SpringBootServletInitializer 接口。

public class SpringBootWarDeploymentExampleApplication extends SpringBootServletInitializer
{
}

步骤12: 覆盖 Configure 方法。

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

SpringBootWarDeploymentExampleApplication.java

package com.w3codebox;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class SpringBootWarDeploymentExampleApplication extends SpringBootServletInitializer
{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) 
{
return application.sources(SpringBootWarDeploymentExampleApplication.class);
}
public static void main(String[] args) 
{
SpringApplication.run(SpringBootWarDeploymentExampleApplication.class, args);
}
}

步骤13: 打开 pom.xml 文件,并将Servlet容器(Tomcat)标记为 已提供.

<scope>provided</scope>
<plugin>
spring-boot-starter-tomcat
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>test</scope>

步骤14: 我们需要部署 WAR 文件,因此在pom.xml文件中将包类型更改为WAR。

<packaging>war</packaging>

步骤15: 使用  修改最终的WAR文件名以标记,以避免包含版本号。我们创建了一个名为 web-services del file WAR.

<build>

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.w3codebox</groupId>
<artifactId>spring-boot-war-deployment-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-boot-war-deployment-example</name>
<description>Demo project for Spring Boot</description>
<parent>
<plugin>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.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>
</properties>
<dependencies>
<scope>provided</scope>
<plugin>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
<scope>provided</scope>
<plugin>
spring-boot-starter-tomcat
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>test</scope>  
<scope>provided</scope>
<plugin>
<dependency>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
 <finalName>web-services</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>     
</plugins>
</build>

</project> Per costruire un'applicazione WAR deployabile su Tomcat, esegui maven clean package. Dopo di che, nel /target/abc.war (dove) abc Supponiamo che l'Id dell'Artifact siaNon indipendente

Applicazione. Passaggio 16: Crea

File WAR":

Clicca con il tasto destro sul progetto-> Esegui come-> 5 Maven Build   Modifica configurazione Un dialogo appare sullo schermo.

Passaggio 17: In  Obiettivoetichetta.  installazione nuovae poi controlla  Salta il test. Clicca su  Applicae  Eseguipulsante.

Dopo aver creato con successo il file WAR, viene visualizzato   Percorso del file WARe messaggi   BUILD SUCCESS Nella console, come illustrato di seguito.

Passaggio 18: copia  percorsoe accedere all'applicazione di   target La cartella. Abbiamo trovato nel cartella di destinazione un file WAR con lo stesso nome specificato nel file pom.xml. Nel nostro esempio, il percorso è:

C:\Utenti\Anubhav\Documenti\workspace-sts-3.9.9.RELEASE\spring-boot-war-deployment-example\target
 

Il deployment aggiunge il file WAR a Tomcat

Per deployare il file WAR, segui i seguenti passaggi:

Passaggio 19: Scarica e installa   Server Apache Tomcat (se non è installato).

Passaggio 20: Copia il file WAR(web-services.war)e incollala inwebappsCartella. Nella nostra esempio, la posizione della cartella webapps è:

C:\Program Files\Apache Software Foundation\Tomcat 8.5\webapps


Passaggio 21: Ora apri il prompt dei comandi e inserisci il seguente comando:

C:\Cd Program Files\Apache Software Foundation\Tomcat 8.5\bin
C:\Cd Program Files\Apache Software Foundation\Tomcat 8.5\bin>startup


AvviaEsegui il comando per avviare il server Tomcat e il file WAR come segue:

L'immagine seguente mostra che il WAR è stato部署成功。

Passaggio 23: Apri il browser e chiama l'URL http://localhost:8080/web-services/hello. It restituisce il messaggio Ciao, utente!.