1. 描述
spring boot 项目是在spring boot的基础上进行项目开发。
两种使用spring boot的方式:
- 继承
- 引入
2. 继承方式
- pom.xml :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<!-- 父项目的pom.xml文件的相对路径; 默认值是../pom.xml。-->
<relativePath />
</parent>
<!--会被子模块继承该依赖-->
<dependencies>
<!-- spring-boot的web启动的jar包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3. 引入方式
- 引入依赖的方式(一般使用这种)
- pom.xml :
<!--声明依赖,子项目若没有显示声明,不会被引入-->
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.5.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- spring-boot的web启动的jar包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
4. 启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// SpringBootApplication 包括了Configuration、EnableAutoConfiguration、ComponentScan三个注解。
@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}
5. 测试类
@Controller
@RequestMapping(value = "/demo")
public class DemoController {
@RequestMapping(value = "/test1", method = {RequestMethod.GET,RequestMethod.POST})
@ResponseBody //返回结果不会被解析为跳转路径,而是直接写入HTTP 响应正文中(ResponseBody)中
public Map test1(){
Map respMap = new HashMap();
respMap.put("code","007");
respMap.put("message","测试demo1");
return respMap;
}
}