参看文档
Create a deployable war file
环境
- JDK8、IDEA、SpringBoot、Gradle
目录结构
步骤
主类继承 SpringBootServletInitializer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.example;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication public class SpringBootWarApplication extends SpringBootServletInitializer {
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringBootWarApplication.class); }
public static void main(String[] args) { SpringApplication.run(SpringBootWarApplication.class, args); } }
|
controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.example.controller;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap; import java.util.Map;
@Controller public class DemoController {
@RequestMapping("demo1") @ResponseBody public Map<String, Object> demo1() { Map<String, Object> map = new HashMap<String, Object>(); map.put("test", "demo1"); return map; } }
|
项目支持 war
编写build.gradle
注意两处war
1 2 3 4 5 6 7 8 9
| apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'spring-boot' apply plugin: 'war'
war { baseName = 'SpringBootWar' version = '0.0.1-SNAPSHOT' }
|
标记内置容器 tomcat
为 provided
1 2 3 4 5
| dependencies { providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' }
|
执行 gradle 中 build
部署 tomcat
war包放入tomcat webapp
中
结果
请求地址需要加上项目解压后的项目名称:localhost:8080/SpringBootWar-0.0.1-SNAPSHOT/demo1,如果想部署根目录,直接把内容剪切到ROOT下