博客
关于我
03 Feign发送Post请求
阅读量:226 次
发布时间:2019-03-01

本文共 2119 字,大约阅读时间需要 7 分钟。

Feign框架处理不同类型请求参数

1 URL传递参数

1.1 服务提供者

在Spring Boot应用中,可以通过@RequestParam注解来接收URL传递的参数。示例代码如下:

@PostMapping("/post")public String test01(@RequestParam String name) {    return name;}

同样,可以接收Map类型的参数:

@PostMapping("/post/map")public String test02(@RequestParam Map
map) { return map.toString();}

1.2 客户端

使用Feign客户端,可以通过@RequestLine注解来定义请求URL,并使用@Param注解来接收URL参数。示例代码如下:

@RequestLine("POST /feign/provider/post?name={name}")String invokeTest01(@Param(value = "name") String name);

同样,可以接收Map类型的参数:

@RequestLine("POST /feign/provider/post/map")String invokeTest02(@QueryMap Map
map);

1.3 测试

测试代码如下:

@Testpublic void test01() {    String response = client.invokeTest01("wade");    System.out.println(response);}
@Testpublic void test02() {    Map
map = new HashMap<>(); map.put("name", "wade"); map.put("age", 28); String response = client.invokeTest02(map); System.out.println(response);}

2 Body参数

2.1 演示

使用@RequestBody注解来接收JSON格式的请求体。示例代码如下:

@PostMapping("/post/body")public String test03(@RequestBody User user) {    return user.toString();}

2.2 API讲解

  • Body注解:用于定义请求体的结构,支持JSON格式数据。需要设置Content-Type请求头参数。
  • Headers注解:用于定义请求头,支持自定义设置。

测试代码如下:

@Testpublic void test03() {    User user = new User();    user.setAge(13);    user.setName("Wade");    String response = client.invokeTest03(user);    System.out.println(response);}

3 Body和URL参数结合使用

3.1 服务提供者

同时接收URL参数和请求体参数。示例代码如下:

@PostMapping("/post/bodyAndUrl")public String test04(@RequestParam Map
map, @RequestBody User user) { return map.toString();}

3.2 客户端

使用@Body注解和@Headers注解来定义请求体和请求头。示例代码如下:

@RequestLine("POST /feign/provider/post/bodyAndUrl")@Body("{body}")@Headers({"content-type: application/json"})String invokeTest04(@QueryMap Map
map, @Param("body") User user);

3.3 测试

测试代码如下:

@Testpublic void test04() {    User user = new User();    user.setAge(13);    user.setName("Wade");    Map
map = new HashMap<>(); map.put("name", "kobe"); map.put("age", 28); String response = client.invokeTest04(map, user); System.out.println(response);}

通过以上方法,可以在Feign框架中高效地处理不同类型的请求参数。

转载地址:http://oprv.baihongyu.com/

你可能感兴趣的文章
pyspider爬虫学习-文档翻译-Architecture.md
查看>>
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your path
查看>>
Pytesseract集字符白名单
查看>>
PyTorch DataLoader 报错 Segmentation fault (core dumped) 的原因及解决方案
查看>>
Pytest ------ 标记函数
查看>>
Pytest Fixture的妙用:深度解析范围和实际运用!
查看>>
Pytest monkeypatch不适用于导入的函数
查看>>
Pytest 命令行报错: Hint: make sure your test modules/packages have valid Python names.
查看>>
pytest 的 request fixture:实现个性化测试需求
查看>>
Pytest+Allure 接口自动化测试平台开发实战
查看>>
pytest+allure使用动态级别,参数化severity
查看>>
Pytest+Unittest+Git+Jenkins企业级CICD自动化测试平台建设方案
查看>>
Pytest+Yaml 数据驱动测试用例
查看>>
pytest-base-url插件之配置可选的项目系统URL
查看>>
pytest-xdist 进行多进程并发测试!
查看>>
pytest-xdist:远程多主机 - 分布式运行自动化测试
查看>>
Pytest中doctests的测试方法应用!
查看>>
Pytest中进行测试环境切换:pytest_addoption!
查看>>
pytest利用request fixture实现个性化测试需求详解
查看>>
pytest单元测试实战
查看>>