Spring Boot删除嵌入式Tomcat服务器,启用Jetty服务器

快速指南,在Spring Boot应用程序中排除嵌入式tomcat服务器并添加Jetty Server。 配置删除tomcat并添加Jetty Server。

1.简介

在本教程中,我们将学习如何从Spring Boot应用程序中删除Tomcat服务器 。 实际上,一旦我们添加了“ spring-boot-starter-web ”依赖项,嵌入式服务器就会默认启动Spring Boot。

但是,Spring Boot使我们可以灵活地使用或不使用tomcat。 如果我们不希望我们可以排除此默认服务器。

默认情况下,Spring Boot带有3种类型的嵌入式服务器:Tomcat,Jetty和undertow。

首先,我们将了解如何排除tomcat和下一步添加jetty服务器

创建第一个Spring Boot应用程序以及如何测试Rest API。

2. Tomcat默认情况下

一旦我们将spring-boot-starter-web依赖关系作为pom.xml的一部分添加到使用spring boot进行Web应用程序开发中,它就会获得tomcat以及所有必需的依赖关系。 直接使用它并自动将其部署到tomcat总是很方便。

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

但是,在某些情况下,当使用JMS而不是Web应用程序或想要添加Jetty时,不需要将tomcat用作Spring Boot应用程序的一部分。

3.排除Tomcat – Maven Pom.xml

要将tomcat从Spring Boot中排除,只需向Spring Boot Starter依赖项添加一个额外的块。 在依赖项部分,我们可以添加
<exclusions>标记,用于确保在构建时删除了给定的工件。

这是最简单的方法。

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

您可以使用该方法将Tomcat从Spring Boot中排除,也可以将其用于其他任何排除

4.排除Tomcat和所有服务器-注释

声明@SpringBootApplication批注时,有一种方法可以排除所有服务器,并且可以考虑像Web一样使用Spring Boot应用程序。

要将spring boot用作非Web应用程序,请使用以下命令。

 @SpringBootApplication (exclude = {EmbeddedServletContainerAutoConfiguration. class , WebMvcAutoConfiguration. class }) 

并且需要将以下属性添加到非休息应用程序,以使Spring Boot不会尝试启动
WebApplicationContext 。 这应该转到application.properties。

 spring.main.web-environment= false 

5.在Spring Boot中添加Jetty Server

如果您想在Spring启动应用程序中使用Jetty服务器,首先必须禁用默认的tomcat服务器,然后添加jetty依赖项“
spring-boot-starter-jetty ”。

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

在pom.xml中添加了jetty之后,在构建时,它将禁用tomcat并映射到Jetty配置。

6. Gradle –排除tomcat并添加Jetty

那么,这很容易。 只需在排除部分添加tomcat,并在依赖项部分添加jetty。

 configurations { 
     compile.exclude module: "spring-boot-starter-tomcat"  }   dependencies { 
     compile( "org.springframework.boot:spring-boot-starter-web:2.0.0.BUILD-SNAPSHOT" ) 
     compile( "org.springframework.boot:spring-boot-starter-jetty:2.0.0.BUILD-SNAPSHOT" )  } 

7.结论

在本文中,我们已经了解了如何通过pom.xml和注释级别禁用tomcat。 如果您注释级别,则它将完全禁用Web应用程序功能。 始终建议使用Maven排除。

并且还了解了如何添加Jetty服务器。

翻译自: https://www.javacodegeeks.com/2020/04/spring-boot-remove-embedded-tomcat-server-enable-jetty-server.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值