添加依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

配置文件application.properties

server.servlet.context-path=/heros

修改包结构

在pom.xml

<packaging>war</packaging>

1

修改启动类

继承SpringBootServletInitializer

重新方法

package com.example.packproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class PackProjectApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(PackProjectApplication.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
//括号里面填 项目启动类.class
return application.sources(PackProjectApplication.class);
}
}

清理

2

打包

3

打包后文件会生成到项目的tager的文件夹下面

上传到服务器

访问:ip+上传的文件名+控制器地址


也可以创建项目时直接选择War包 ,如果直接选择War包的话 只需要在配置是配置一个路径

也就是上面的第二部

server.servlet.context-path=/heros

启动类它会帮你自动从新生成一个 只需要把继承后的复制到自己项目的启动类上 ,然后把下面的重写的方法 复制到自己项目的启动类就行了