分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
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"作为前缀。下面举一些常用的例子。
- 1
配置servlet容器
server.port = #配置程序端口,默认为8080server.session-timeout=#用户session过期,以秒为单位server.context-path= #配置访问路径,默认为/
- 1
- 2
- 3
配置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
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
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
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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;@Componentpublic class CustomServletContainer implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(8888); container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html")); container.setSessionTimeout(10, TimeUnit.MINUTES); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
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@SpringBootApplicationpublic class Ch74Application { @RequestMapping("/") @ResponseBody private String hello() { return "hello!"; } @RequestMapping("/toIndex") public String toIndexPage() { return "index1"; } public static void main(String[] args) { SpringApplication.run(Ch74Application.class,args); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
1.2.7 运行
浏览器中输入:http://localhost:8888/toIndex
浏览器中输入:http://localhost:8888,最后的效果如下: