当前位置: 首页 > news >正文

spring boot中web容器配置

web容器配置

spring boot 默认的web容器是 tomcat,如果需要换成其他的 web 容器,可以如下配置。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><!-- 默认使用的是 tomcat,这里做个排除,使用下面配置的jetty --><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions>
</dependency><!--支持 servlet 的容器有三个 tomcat,jetty,UndertowUndertow 是 Red Hat 公司的开源产品, 它完全采用 Java 语言开发,是一款灵活的高性能 Web 服务器,支持阻塞 IO 和非阻塞 IO。由于 Undertow 采用 Java 语言开发,可以直接嵌入到 Java 项目中使用。同时, Undertow 完全支持 Servlet 和 Web Socket,在高并发情况下表现非常出色。Jetty does not yet support Servlet 6.0. To use Jetty with Spring Boot 3.0, you will have to downgrade the Servlet API to 5.0.<dependency><groupId>jakarta.servlet</groupId><artifactId>jakarta.servlet-api</artifactId><version>5.0.0</version></dependency>-->
<dependency>     <groupId>org.springframework.boot</groupId><!--<artifactId>spring-boot-starter-tomcat</artifactId><artifactId>spring-boot-starter-undertow</artifactId>--><artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<!---->

如果使用的 reactive 的话,tomcat,jetty,Undertow 之外还可以选择 netty。

正常来说,spring boot 会根据是否有spring-boot-starter-web来确定当前是项目是一个web项目(servlet还是reactive),还是一个javase项目,同时还可以在配置文件中指定是否启动web容器,或者容器的类型。
在这里插入图片描述
spring boot 端口配置

server:# 指定端口号port: 8888# 关闭 http 请求port: -1# 随机端口port: 0

如果是随机端口,可以通过自定义监听器来获取端口,然后使用。

import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;public class PortApplicationListener implements ApplicationListener<WebServerInitializedEvent> {@Overridepublic void onApplicationEvent(WebServerInitializedEvent event) {System.out.println("event.getWebServer().getPort() = " + event.getWebServer().getPort());}
}

配置 spring boot 的相应压缩,一般用不到,实际使用中通过 nginx 来做返回内容的压缩即可。

server:port: 8888compression:enabled: true# 大于 2kb 的内容进行压缩min-response-size: 2# 压缩的文件类型mime-types: application/fastsoap
http://www.lryc.cn/news/112222.html

相关文章:

  • DNSlog注入(利用DNSlog平台将SQL盲注变成回显注入)
  • vim学习笔记(致敬vim作者)
  • 力扣 -- 139. 单词拆分
  • 百度秋招攻略,百度网申笔试面试详解
  • nohup Java -jar 生成的nohup.out 文件一直增加,如何处理
  • 静态页面与动态页面的区别及部署jpress应用
  • 华为数通HCIA-华为VRP系统基础
  • 基于Azure OpenAI Service 的知识库搭建实验⼿册
  • 第七节--结构体
  • Docker学习(二十四)报错速查手册
  • 一种SpringBoot下Scheduler定时任务优雅退出方案
  • DNS部署与安全详解(上)
  • 【51单片机】晨启科技,酷黑版,音乐播放器
  • 基于SPSSPRO实现层次分析法(AHP)
  • Spring Test中使用MockMvc进行上传文件单元测试时,报NullPointerException
  • HTTP常用状态码及其含义
  • FFmpeg中AVIOContext的使用
  • 【react】react中BrowserRouter和HashRouter的区别:
  • 机器学习常用Python库安装
  • HTTP 劫持、DNS 劫持与 XSS
  • bash引用-Quoting详细介绍
  • powershell几句话设置环境变量
  • Javascript 数据结构[入门]
  • IO(JavaEE初阶系列8)
  • React Native 样式表的基础知识
  • 【JS 解构赋值】
  • Vue3状态管理库Pinia——自定义持久化插件
  • il汇编整数相加
  • RabbitMQ 事务
  • vue前端 让年月日 加上23:59:59