English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Dans cette section, nous allons apprendre à déployer une application Spring Boot sur le serveur Tomcat.
Il comprend trois étapes:
Configurer l'application Spring Boot 创建Spring Boot WAR Déployer un WAR sur Tomcat
Laissons-nous créer un exemple Maven que nous pourrons déployer sur Tomcat.
Configurer l'application Spring Boot
步骤1: Ouvrir Spring Initializr http: //start.spring.io .
步骤2: fournir Group Nom. Nous avons fourni com.w3codebox.
步骤3: fournir Artifacts ID. Nous avons fourni spring-.RELEASE\spring-boot-war-example。
步骤4: Ajouter Spring Web Dépendances
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-.RELEASE\spring-starter-web</artifactId> </dependency>
步骤5: Cliquez sur Générer (Généré) bouton. Il encapsule toutes les spécifications associées au projet et les télécharge dans notre système local. jar 文件。
步骤6: Extracter fichiers jar.
步骤7: Importer,veuillez suivre les étapes suivantes:
Fichier->Importer->Projet Maven existant->Suivant->Parcourir->Sélectionner le dossier du projet->Terminer
Après avoir importé le projet, nous pouvons dans l'IDE Explorateur de packages On peut voir la structure du répertoire suivante dans la partie.
步骤8: Dans le paquet com.w3codebox Dans le répertoire de création de la classe Controller. Nous avons créé un nom de classe Classe DemoRestController.
Dans la classe de contrôleur, nous avons défini une méthode retournant une chaîne de caractères. 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 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)标记为 已提供。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-.RELEASE\spring-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
步骤14: 我们需要部署 WAR 文件,因此在pom.xml文件中将包类型更改为WAR。
<packaging>war</packaging>
步骤15: 使用
<finalName>web-services</finalName>
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-.RELEASE\spring-boot-war-example</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>spring-.RELEASE\spring-boot-war-example</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-.RELEASE\spring-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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-.RELEASE\spring-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-.RELEASE\spring-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-.RELEASE\spring-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <finalName>web-services</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-.RELEASE\spring-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Pour construire une application WAR déployable sur Tomcat, nous exécutons maven clean package. Après cela, dans /target/abc.war (où abc supposé être Artifact Id) générer notre fichier WAR. Nous devons considérer que ce nouveau réglage rend notre application Spring Boot Non indépendantapplication.
步骤16: Créer Fichier WAR :
Cliquez-droit sur le projet-> Exécuter comme-> 5 Maven Build un Éditer la configuration Une boîte de dialogue apparaît à l'écran. n°17étape : dans cibleet écrivez installation complètepuis vérifiez Ignorer les testsCliquez séparément sur Applicationet Exécuterbouton. Après la création réussie du fichier WAR, il affiche chemin du fichier WARet messages BUILD SUCCESS dans la console, comme indiqué dans l'image ci-dessous. 步骤18: Copier cheminet accéder à l'application via target Dossier. Nous avons trouvé dans le dossier cible un fichier WAR ayant le même nom que celui spécifié dans le fichier pom.xml. Dans notre exemple, le chemin est : 部署:将 WAR 文件添加到 Tomcat 步骤19: 要部署 WAR 文件,请按照以下步骤操作: 下载并安装 Apache Tomcat ServerC:\Users\Anubhav\Documents\workspace-C:\Users\Anubhav\Documents\workspace-3.9.9sts-.RELEASE\spring-boot-war-deployment
example\target
步骤20:(如果未安装) 复制 WAR 文件(web-services.war)并将其粘贴到
C:\Program Files\Apache Software Foundation\Tomcat 8.5\webapps
步骤21: 现在打开命令提示符并键入以下命令:
C:\Program Files\Apache Software Foundation\Tomcat 8.5\bin C:\Program Files\Apache Software Foundation\Tomcat 8.5\bin>startup
启动以下命令启动 Tomcat 服务器并部署 WAR 文件:
下图显示了已成功部署的 WAR。
步骤23: 打开浏览器并调用 URL http://localhost:8080/web-services/hello。它返回消息 您好,用户好!。