1、修改pom.xml文件
①设置打包形式
<packaging>war</packaging>
②移除嵌入式Tomcat插件
修改前:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
修改后:
<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>
③添加servlet-api依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
2、修改Application启动类
修改前:
@SpringBootApplication
public class KlausApplication {
public static void main(String[] args) {
SpringApplication.run(KlausApplication.class, args);
}
}
修改后:
@SpringBootApplication
public class KlausApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(KlausApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(KlausApplication.class);
}
}
3、将war包放入外部tomcat的webapps下
4、在外部Tomcat的bin目录下双击startup.bat
5、访问方式:输入http://localhost:8080/项目名称/接口地址
问题描述:SpringBoot项目部署到Tomcat中访问接口出现404
问题原因:地址输入时项目名称不是tomcat中部署的war包名称
解决方法:修改问题地址
问题地址:http://localhost:8080/Klasu/
成功地址:http://localhost:8080/klaus-0.0.1-SNAPSHOT/
Dream is not impetuous, but precipitation and accumulation.