SpringBoot中Tomcat配置(学习SpringBoot实战)
1、Tomcat配置
Spring Boot默认内嵌的Tomcat为Servlet容器,所以本节只讲对Tomcat配置,其实本节的配置对Tomcat、Jetty和Undertow都是通用的。
1.1 配置Tomcat
关于Tomcat的所有属性都在org.springframework.boot.autoconfigure.web.ServerProperties配置类中做了定义,我们只需在application.properties配置属性做配置即可。通用的Servlet容器配置都以"server"作为前缀,而Tomcat特有配置都以"server.tomcat"作为前缀。下面举一些常用的例子。
配置servlet容器
server.port = #配置程序端口,默认为8080
server.session-timeout=#用户session过期,以秒为单位
server.context-path= #配置访问路径,默认为/
配置Tomcat
server.tomcat-uri-encoding = #配置Tomcat编码,默认为UTF-8
server.tomcat.compression = #Tomcat是否开启压缩,默认为关闭off
1.2 代码配置Tomcat
如果你需要通过代码的方式配置servlet容器,则可以注册一个实现EmbeddedServletContainerCustomizer接口的Bean,若想直接配置Tomcat、Jetty、Undertow,则可以直接定义TomcatEmbeddedServletContainerFactor、JettyEmbeddedServletContainerFactor、UndertowEmbeddedServletContainerFactor。
1.2.1 编写案例,项目目录如下
1.2.2 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.wisely</groupId><artifactId>ch7_4</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>ch7_4</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.0.M1</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></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><repositories><repository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/snapshot</url><snapshots><enabled>true</enabled></snapshots></repository><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/snapshot</url><snapshots><enabled>true</enabled></snapshots></pluginRepository><pluginRepository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories></project>
1.2.3 index.html的内容
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>Insert title here</title>
</head>
<body>
index page
</body>
</html>
1.2.4 404.html
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>Insert title here</title>
</head>
<body>
page not found,this is 404 page!
</body>
</html>
1.2.5 CustomServletContainer.java的内容
package com.wisely.ch7_4;import ch.qos.logback.core.util.TimeUtil;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.ErrorPage;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;import java.util.concurrent.TimeUnit;@Component
public class CustomServletContainer implements EmbeddedServletContainerCustomizer {@Overridepublic void customize(ConfigurableEmbeddedServletContainer container) {container.setPort(8888);container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));container.setSessionTimeout(10, TimeUnit.MINUTES);}
}
1.2.6 Ch74Application.java的内容
package com.wisely.ch7_4;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;@Controller
@SpringBootApplication
public class Ch74Application {@RequestMapping("/")@ResponseBodyprivate String hello() {return "hello!";}@RequestMapping("/toIndex")public String toIndexPage() {return "index1";}public static void main(String[] args) {SpringApplication.run(Ch74Application.class,args);}
}
1.2.7 运行
浏览器中输入:http://localhost:8888/toIndex
浏览器中输入:http://localhost:8888,最后的效果如下:
SpringBoot中Tomcat配置(学习SpringBoot实战)相关推荐
- SpringBoot中如何配置使用过滤器(Filter)呢?
转自: SpringBoot中如何配置使用过滤器(Filter)呢? 下文笔者讲述springboot中配置过滤器的方法分享,如下所示 实现思路:1.定义filter2.将filter注册进sprin ...
- springboot中@Configuration配置类加载流程
springboot中@Configuration配置类加载流程 代码位置 源码解读 每一步的分析 代码位置 ConfigurationClassParser#doProcessConfigurati ...
- SpringBoot切换Tomcat容器,SpringBoot使用Jetty容器
转载自 https://www.cnblogs.com/fanshuyao/p/8668059.html SpringBoot切换Tomcat容器, SpringBoot修改为Jetty容器, Spr ...
- idea中tomcat配置详解
idea中tomcat配置详解 本篇文章主要介绍在idea下配置maven.tomcat.jdk的开发环境. 详细步骤: 1.Edit Configurations 2.Add New Tomcat ...
- SpringBoot中oauth2.0学习之服务端配置快速上手
现在第三方登录的例子数见不鲜.其实在这种示例当中,oauth2.0是使用比较多的一种授权登录的标准.oauth2.0也是从oauth1.0升级过来的.那么关于oauth2.0相关的概念及其原理,大家可 ...
- 从源码剖析SpringBoot中Tomcat的默认最大连接数
为什么你的websocket只能建立256个连接?推出后,有许多小伙伴问:关键是怎么解决256这个问题.嗯,可能是我的标题起的有点问题,不过如果有认真阅读文章的话,应该会知道,其实256的限制是Chr ...
- SpringBoot中Profile配置和加载配置文件
文章目录 一.多Profile的资源文件 二.profile激活 1.配置文件方式激活profile 2.命令行方式激活profile 三.@profile使用 写在前面: 我是「境里婆娑」.我还是从 ...
- 数据源(DataSource)是什么以及SpringBoot中数据源配置
数据源 数据源,简单理解为数据源头,提供了应用程序所需要数据的位置.数据源保证了应用程序与目标数据之间交互的规范和协议,它可以是数据库,文件系统等等.其中数据源定义了位置信息,用户验证信息和交互时所需 ...
- SpringBoot修改tomcat配置
SpringBoot修改tomcat相关配置 方式一:修改和server有关的配置(ServerProperties[也是EmbeddedServletContainerCustomizer]): / ...
最新文章
- matlab通信物理层仿真,通信小精灵(物理层仿真工具) 可计算仿真误码率、理论误...
- BootStrap table 数据填充与分页应用总结
- 十 ubus安装编译
- python中str和repr_python中str()和repr()函数的区别
- 苹果六电池_昆明苹果手机售后维修地址 昆明苹果手机维修哪家好?
- 机器学习 文本分类 代码_无需担心机器学习-如何在少于10行代码中对文本进行分类
- ubuntu18安装python3.6.8_ubuntu 18.04 + Python 3.6.8 更换软件安装源
- (android之sqlite三)单机Sqlite数据库
- js高级学习笔记(b站尚硅谷)-8-关于语句分号的问题
- 数据分析与R语言视频教程
- java 多线程高级面试_15个顶级Java多线程面试题及答案
- RS485收发的3种典型电路-重点-自动收发电路
- android判断是否是蓝牙耳机,如何验证蓝牙耳机是否在Android上连接?
- lamp一键安装包+linux,linux下的lanmp/lamp/lnmp一键安装包
- python分位数回归模型_分位数回归森林
- 8.0系统手机无需Root激活XPOSED框架经验
- 计算机网络实验 Go Back N (带有ACK)滑动窗口协议 C++
- HDU 1248(寒冰王座)
- 二值形态学之击中击不中变换
- UNETR 医学图像分割架构 2D版 (Tensorflow2 Keras 实现UNETR)
热门文章
- Java打印菱形(空格菱形)(星星之间有空格)
- 电气论文实现:通过电力光伏负荷预测讲解seq2seq翻译模型
- python太阳花绘制
- Spring Boot 日志管理
- JavaScript实现截留雨水问题的蛮力方法的算法(附完整源码)
- wxWidgets:wxRichTextStyleListCtrl类用法
- wxWidgets:wxMediaCtr类用法
- boost::detail::yield相关的测试程序
- boost::mpl::string相关的测试程序
- 使用 Boost.MPI 的 reduce() 计算最小值的示例