spring boot项目中 pom.xml 的两种配置

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值