猿创征文|搭建第一个Spring Boot项目

IDEA 旗航版本新建SpringBoot项目,file-new-Project-Spring Initializr。

当然,这一步也可以构建Maven来创建框架,新建项目一样

场景依赖选择界面

默认项目包名

pom.xml依赖文件

添加spring-boot-starter-parent依赖是Spring Boot框架集成项目的统一父类管理依赖,添加该依赖后可以使用Spring Boot的相关特性,<version>是指Spring Boot的版本号

父项目做依赖项目 
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
</parent>

添加spring-boot-starter-web依赖是SpringBoot框架对web开发场景集成支持的依赖启动器,添加该依赖后就可以自动导入Spring MVC框架相关依赖进行web开发

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

项目打包插件,直接在服务器执行

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

遇到bug:

在/src/main/java开发目录下创建主程序MainApplication类,@SpringBootApplication注释是SpringBoot用于MainApplication类作为主程序启动类

/**
 * 主程序类
 * @SpringBootApplication:这是一个SpringBoot应用
 */
@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}

创建HelloController的请求处理控制类编写业务

@RestController
public class HelloController {


    @RequestMapping("/hello")
    public String handle01(){
        return "Hello, Spring Boot 2!";
    }


}

运行项目

单元测试

实际开发中,每当完成一个功能接口或业务方法的编写后,在测试类检验该功能是否正确,添加spring-boot-starter-test测试依赖启动器

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

编写单元测试类和测试方法

在/src/test/java测试目录下创建与项目主程序对应的单元测试类

import com.atguigu.boot.controller.HelloController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MainApplicationTests {
    @Test
    public void contextLoabs(){

    }
    @Autowired
   private HelloController helloController;
        public void HelloControllerTest(){
            String hello = helloController.handle01();
            System.out.println(hello);
        }
}

热部署

通常会对一段业务不断地修改测试,在修改之后往往需要重新启动服务,有些服务启动需要加载很长时间才能启动成功,添加spring-boot-devtools热部署依赖启动,测试效果,在不关闭当前项目的情况下,修改HelloController的handle01()方法返回值,刷新浏览器。

  • 29
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 20
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值