Springboot | 入门01

Springboot | 入门01

什么是Springboot

Springboot官网:https://spring.io/projects/spring-boot

Springboot官网对Springboot的介绍:

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.

简单来说,Spring Boot是一个基于Spring的套件,它帮我们预组装了Spring的一系列组件,以便以尽可能少的代码和配置来开发基于Spring的Java应用程序。

以汽车为例,如果我们想组装一辆汽车,我们需要发动机、传动、轮胎、底盘、外壳、座椅、内饰等各种部件,然后把它们装配起来。Spring就相当于提供了一系列这样的部件,但是要装好汽车上路,还需要我们自己动手。而Spring Boot则相当于已经帮我们预装好了一辆可以上路的汽车,如果有特殊的要求,例如把发动机从普通款换成涡轮增压款,可以通过修改配置或编写少量代码完成。

Springboot的目录结构

springboot-hello
├── pom.xml
├── src
│   └── main
│       ├── java
│		    └── com
│			   └── example
│			      └── springboot-hello
│			         └── springboothelloApplication.java
│       └── resources
│ 	
│           ├── application.properties
│           ├── static
│           └── templates
│	└── test
└── target

介绍几个重点文件/目录

  • src/java 编写核心逻辑的地方
  • application.properties Spring Boot默认的配置文件
  • pom.xml Maven项目配置文件
  • springboothelloApplication.java 是Spring Boot的启动类

快速启动springboot

Application启动类:

@SpringBootApplication
public class springboothelloApplication {

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

直接运行这个类

之后可以看到Springboot的日志


现在,我们在浏览器输入localhost:8080就可以直接访问页面

测试工具Apifox

当编写好springboot程序后,需要有工具的测试接口。

Apifox 是集 API 文档、API 调试、API Mock、API 自动化测试多项实用功能为一体的 API 管理平台

Apifox官方文档:https://apifox.com/help/

Demo 用apifox测试Springboot接口

需求:编写简单的sprintboot,利用Apifox测试接口

在项目根目录下新建packagecontroller,再新建HelloController.java

编写代码:


@RestController
public class HelloController {
    private static HashMap<String, Integer> a = new HashMap<>();

    @GetMapping("hello")
    public String getHello() {
        return "Hello Springboot:"+a.toString();
    }

    @PostMapping("/hello")
    public String postHello(String name, int age) {
        a.put(name, age);
        return "这是一个post请求:" + "name是" + name + "age是" + age;
    }

    @PutMapping("/hello")
    public String putHello(String name, int age) {
        a.replace(name, age);
        return "这是一个put请求:" + "name是" + name + "age是" + age;
    }

    @DeleteMapping("/hello/{name}")
    public String deleteHello(@PathVariable String name) {
        a.remove(name);
        return "这是一个delete请求:" + "name是" + name;
    }
}

利用apifox发送请求


同理其他请求

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值