springboot tomcat的demo
时间: 2023-11-04 22:58:08 浏览: 131
这里提供一个简单的Spring Boot集成Tomcat的示例,可以作为参考:
1. 首先,在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排除内嵌Tomcat -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加Tomcat依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.41</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.41</version>
</dependency>
```
2. 创建一个Spring Boot Application,并继承`SpringBootServletInitializer`类,以支持WAR部署:
```java
@SpringBootApplication
public class TomcatDemoApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(TomcatDemoApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(TomcatDemoApplication.class, args);
}
}
```
3. 创建一个Controller类,提供一个简单的API接口:
```java
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Tomcat!";
}
}
```
4. 配置Tomcat相关参数,可以在`application.properties`文件中配置:
```properties
server.port=8080
server.tomcat.max-threads=200
server.tomcat.uri-encoding=UTF-8
```
5. 运行Spring Boot应用程序,访问`http://localhost:8080/hello`即可看到结果。
注意:在使用Tomcat作为嵌入式服务器时,不需要在应用程序中添加`@EnableWebMvc`注解,否则会导致默认的Spring Boot配置失效。
阅读全文