當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot中Tomcat配置(学习SpringBoot实战)
生活随笔
收集整理的這篇文章主要介紹了
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之SSL配置(Spr