使用IDEA自带的Http Client进行接口调试

新建一个SpringBoot项目,且启动正常

关于如何新建一个SpringBoot项目,可参考这儿
在这里插入图片描述

定义接口

在这里插入图片描述

  • 接口用于GET请求
//TestInterfaceA.java
package com.jepcc.demo.config;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestInterfaceA {
    @GetMapping("/test/a")
    public String testA(){
        return "Hello World!";
    }
}

启动应用,打开浏览器输入localhost:8080/test/a,显示Hello World!,接口正常。

  • 接口用于POST请求
//TestInterfaceB.java
package com.jepcc.demo.config;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestInterfaceB {
    @PostMapping("/test/b")
    public String testB(String name){
        return "Hello,"+name;
    }
}
使用IDEA自带的Http Client测试接口
  • 在项目根目录下新建目录http,在http目录下新建文件test.http(注意哈,文件后缀一定要是.http
    在这里插入图片描述
  • 定义GET请求,用于访问/test/a接口
GET http://localhost:8080/test/a
Accept: application/json
### 
  • 定义POST请求,用于访问/test/b接口
POST http://localhost:8080/test/b
Content-Type: application/x-www-form-urlencoded

name=Nicholas
###

在这里插入图片描述

  • 执行接口调用
    在这里插入图片描述
接口单元测试

针对接口/test/a,编写单元测试代码。

GET http://localhost:8080/test/a
#Accept: application/json

> {%
 client.test("test-a",function(){
     client.log("Testing Interface:/test/a");
     client.log(response.body);
     client.assert(response.status===200,"response status is not 200");
     client.assert(response.body==="Hello World!","response body is not \"Hello World!\"");
 })
 %}
###

需要注意的是,记得 “空一行空一格”。
在这里插入图片描述
在这里插入图片描述

接口调用历史记录
  • Tools>Http Client>Show Http Requests History,可查看Http请求历史记录 在这里插入图片描述
  • 应用根目录下的.idea>httpRequests中也保存着Http请求历史记录
    在这里插入图片描述
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值