1.快速创建一个springboot项目,勾选web的依赖
2.创建一个测试controller,并测试接口
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping("")
@ResponseBody
public Map test(){
Map map = new HashMap();
map.put("id","123456");
map.put("title","iphone");
return map;
}
}
3.使用外部Tomcat,需要排除内置的Tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
4.修改打包方式为war包
<packaging>war</packaging>
5.修改启动类,继承SpringBootServletInitializer 类,并覆盖 configure 方法
@SpringBootApplication
public class SpringboottomcatApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringboottomcatApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringboottomcatApplication.class, args);
}
}
6.添加servlet-api 的相关 jar 包,SpringBootServletInitializer 类需要用到
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
7.idea工具,添加外部Tomcat,并添加部署包到Tomcat中(省略)
8.启动Tomcat,并测试创建项目添加的测试接口