SpringBoot项目开发完毕后,支持两种方式部署到服务器:
- 一、创建Spring Boot工程
- 1、创建工程
- 2、创建UserController
- 二、jar包(官方推荐)
- 1、打包
- 2、运行jar包
- 三、war包
- 1、修改pom.xml的打包方式
- 2、修改核心启动类`SpringbootDeployApplication`去继承`SpringBootServletInitializer`重写`configure`方法
- 3、打包
- 4、运行war包
- 5、指定打包名称(上述打包的名称太长)
package cn.itbluebox.springbootdeploy;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/user")
@RestController
public class UserController {
@RequestMapping("/findAll")
public String findAll(){
return "SUCCESS";
}
}
二、jar包(官方推荐)
1、打包
打包成功,以及对应jar的位置
jar包的位置
D:AProgramWorkSpaceideatestspringboot-deploytargetspringboot-deploy-0.0.1-SNAPSHOT.jar
我们发现其位置就在当前项目的target目录下
在jar所在目录的地址栏内输入cmd
输入:java -jar springboot-deploy-0.0.1-SNAPSHOT.jar
访问:http://localhost:8080/user/findAll
war打包后对应配置文件application.properties当中的server.port会失效,如果需要修改端口号需要去Tomcat的配置文件当中设置
1、修改pom.xml的打包方式2、修改核心启动类SpringbootDeployApplication去继承SpringBootServletInitializer重写configure方法war
package cn.itbluebox.springbootdeploy;
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 SpringbootDeployApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringbootDeployApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringbootDeployApplication.class);
}
}
3、打包
对应war 的路径
D:AProgramWorkSpaceideatestspringboot-deploytargetspringboot-deploy-0.0.1-SNAPSHOT.war
我们发现其位置就在当前项目的target目录下
将war放到Tomcat的安装目录的wabapps的目录下
启动Tomcat,在bin目录下的startup.bat
启动成功
访问:http://localhost:8080/springboot-deploy-0.0.1-SNAPSHOT/user/findAll
修改pom.xml
打包
将war放到Tomcat的安装目录的wabapps的目录下
放入后不用重新启动Tomcat自动会解压对应war
访问http://localhost:8080/springboot/user/findAll



