一、tomcat版本:8.0以上
二、修改打包方式,将pom.xml中的packaging的jar改为war
原来:
1 <groupId>com.shop</groupId> 2 <artifactId>shop</artifactId> 3 <version>0.0.1-SNAPSHOT</version> 4 <packaging>jar</packaging>
修改后:
1 <groupId>com.shop</groupId> 2 <artifactId>shop</artifactId> 3 <version>0.0.1-SNAPSHOT</version> 4 <packaging>war</packaging>
三、去除springboot内置的tomcat
pom.xml代码如下:
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 <exclusions> 5 <exclusion> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-tomcat</artifactId> 8 </exclusion> 9 </exclusions> 10 </dependency>
四、加入servlet-api
1 <dependency> 2 <groupId>javax.servlet</groupId> 3 <artifactId>javax.servlet-api</artifactId> 4 <scope>provided</scope> 5 </dependency>
五、修改启动类,并重写初始化方法
原来的启动类,类似如下:
1 package com.shop; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class ShopApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(ShopApplication.class, args); 11 } 12 }
让启动类继承SpringBootServletInitializer,重写初始化方法:
1 package com.shop; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.boot.builder.SpringApplicationBuilder; 6 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 7 8 @SpringBootApplication 9 public class ShopApplication extends SpringBootServletInitializer { 10 11 public static void main(String[] args) { 12 SpringApplication.run(ShopApplication.class, args); 13 } 14 15 @Override 16 protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 17 return builder.sources(this.getClass()); 18 } 19 }
六、导出war包放入tomcat的webapps目录下,启动tomcat进行测试:
七、部署在tomcat7.0会报错(具体原因待考究,欢迎各路老鸟指正),此项目为练习项目,只有一个启动类和一个controller类。