Feign 系列(01)最简使用姿态
Feign 系列(01)最简使用姿态
Spring Cloud 系列目录(https://www.cnblogs.com/binarylei/p/11563952.html#feign)
更多使用案例见 Feign Github 官网
1. 引入 maven 依赖
<dependencies>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>10.4.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-gson</artifactId>
<version>10.4.0</version>
</dependency>
</dependencies>
2. 基本用法
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
@RequestLine("POST /repos/{owner}/{repo}/issues")
void createIssue(Issue issue, @Param("owner") String owner, @Param("repo") String repo);
}
public static class Contributor {
String login;
int contributions;
}
public static class Issue {
String title;
String body;
List<String> assignees;
int milestone;
List<String> labels;
}
public class MyApp {
public static void main(String... args) {
GitHub github = Feign.builder()
.decoder(new GsonDecoder())
.target(GitHub.class, "https://api.github.com");
// Fetch and print a list of the contributors to this library.
List<Contributor> contributors = github.contributors("OpenFeign", "feign");
for (Contributor contributor : contributors) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
}
}
}
总结: Feign.target()
实际上是创建了一个 GitHub 的动态代理。
3. Feign 声明式注解
Feign 通过 Contract
接口将方法上标注的注解解析成 MethodMetadata,最终将参数解析成 Http 请求的请求行、请求行、请求体,然后使用 HttpClient 发送请求。
Annotation | Interface Target | Usage |
---|---|---|
@RequestLine |
Method | 定义HttpMethod 和 UriTemplate. UriTemplate 中使用{} 包裹的表达式,可以通过在方法参数上使用@Param 自动注入 |
@Param |
Parameter | 定义模板变量,模板变量的值可以使用名称的方式使用模板注入解析 |
@Headers |
Method, Type | 定义头部模板变量,使用@Param 注解提供参数值的注入。如果该注解添加在接口类上,则所有的请求都会携带对应的Header信息;如果在方法上,则只会添加到对应的方法请求上 |
@QueryMap |
Parameter | 定义一个Map或 POJO,参数值将会被转换成URL上的 query 字符串上 |
@HeaderMap |
Parameter | Map ->Http Headers |
@Body |
Method | Defines a Template , similar to a UriTemplate and HeaderTemplate , that uses @Param annotated values to resolve the corresponding Expressions . |
注解的基本使用方法如下:
public interface FeignService {
// @Headers
@RequestLine("GET /api/documents/{contentType}")
@Headers("Accept: {contentType}")
String getDocumentByType(@Param("contentType") String type);
// @QueryMap: Map or POJO
@RequestLine("GET /find")
V find(@QueryMap Map<String, Object> queryMap);
@RequestLine("GET /find")
V find(@QueryMap CustomPojo customPojo);
// @HeaderMap: Map
@RequestLine("POST /")
void post(@HeaderMap Map<String, Object> headerMap);
// @Body
@RequestLine("POST /")
@Headers("Content-Type: application/xml")
@Body("<login \"user_name\"=\"{user_name}\" \"password\"=\"{password}\"/>")
void xml(@Param("user_name") String user, @Param("password") String password);
@RequestLine("POST /")
@Headers("Content-Type: application/json")
@Body("%7B\"user_name\": \"{user_name}\", \"password\": \"{password}\"%7D")
void json(@Param("user_name") String user, @Param("password") String password);
}
每天用心记录一点点。内容也许不重要,但习惯很重要!
Feign 系列(01)最简使用姿态的更多相关文章
- Eureka 系列(01)最简使用姿态
目录 Eureka 系列(01)最简使用姿态 0. Spring Cloud 系列目录 - Eureka 篇 1. 服务发现与发现 1.1 服务发现(Service Discovery) 1.2 服务 ...
- Feign 系列(05)Spring Cloud OpenFeign 源码解析
Feign 系列(05)Spring Cloud OpenFeign 源码解析 [TOC] Spring Cloud 系列目录(https://www.cnblogs.com/binarylei/p/ ...
- java io系列01之 "目录"
java io 系列目录如下: 01. java io系列01之 "目录" 02. java io系列02之 ByteArrayInputStream的简介,源码分析和示例(包括 ...
- SAP接口编程 之 JCo3.0系列(01):JCoDestination
SAP接口编程 之 JCo3.0系列(01):JCoDestination 字数2101 阅读103 评论0 喜欢0 JCo3.0是Java语言与ABAP语言双向通讯的中间件.与之前1.0/2.0相比 ...
- Java 集合系列 01 总体框架
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- Java 之 I/O 系列 01 ——基础
Java 之 I/O 系列 目录 Java 之 I/O 系列 01 ——基础 Java 之 I/O 系列 02 ——序列化(一) Java 之 I/O 系列 02 ——序列化(二) 整理<疯狂j ...
- JavaScript进阶系列01,函数的声明,函数参数,函数闭包
本篇主要体验JavaScript函数的声明.函数参数以及函数闭包. □ 函数的声明 ※ 声明全局函数 通常这样声明函数: function doSth() { alert("可以在任何时候调 ...
- 委托、Lambda表达式、事件系列01,委托是什么,委托的基本用法,委托的Method和Target属性
委托是一个类. namespace ConsoleApplication1 { internal delegate void MyDelegate(int val); class Program { ...
- [.NET MVC4 入门系列01]Helloworld MVC 4 第一个MVC4程序
[.NET MVC4 入门系列01]Helloworld MVC 4 第一个MVC4程序 一.练习项目: http://www.asp.net/mvc/tutorials/mvc-4/gettin ...
随机推荐
- MyEclipse增强代码补全
MyElipse的默认代码提示功能隐藏了许多细节,需要开发者手动设置,一起来设置吧,让你的myeclpse更强大. 方法 1 打开MyEclipse 6.0.1,然后“window”→“Prefere ...
- 28. Python编写自动化测试用例
接口文档已经提供了,requests库.unittest单元测试框架也已经介绍过,笔者相信读者朋友已经可以独立编写接口自动化测试用例了.但是有一些细节,我们需要聊一下.比如我们写登录接口测试用例,用户 ...
- C++ STL rope 可持久化平衡树 (可持久化数组)
官方文档好像 GG 了. rope 不属于标准 STL,属于扩展 STL,来自 pb_ds 库 (Policy-Based Data Structures). 基本操作: #include <e ...
- Java web后台插入数据库中文乱码问题解决
前言:项目想要避免乱码情况的出现,要保持服务器.数据库.项目.以及前端编码一致.一.项目以及前端编码,设置. myeclipse--->右键项目--->Properties--->R ...
- 数据库全表扫描的SQL种类
1.所查询的表的条件列没有索引: 2.需要返回所有的行: 3.对索引主列有条件限制,但是使用了函数,则Oracle 使用全表扫描,如: where upper(city)=’TOKYO’; 这样的语 ...
- web服务器和应用服务器以及web应用框架介绍
### web服务器:负责处理http请求,响应静态文件,常见的有Apache,Nginx以及微软的IIS. ### 应用服务器:负责处理逻辑的服务器.比如php.python的代码,是不能直接通过n ...
- Python之字典中的键映射多个值
字典的键值是多个,那么就可以用列表,集合等来存储这些 键值 举例 print({"key":list()}) # {'key': []} print({"key" ...
- docker部署一个简单的mian.py项目文件
安装docker yum install -y docker 启动docker systemctl start docker 查询可安装的Python版本,默认centos python 2.7 ...
- 为什么要用webpack!
为什么要用webpack? 现今的很多网页其实可以看做是功能丰富的应用,它们拥有着复杂的JavaScript代码和一大堆依赖包. 模块化,让我们可以把复杂的程序细化为小的文件; 类似于Type ...
- Jmeter服务器性能压测-用户登录实例CSV方式
为什么用CSV方式压测,因为用jdbc链接数据库,我发现数据库数据量量大的情况下,Jmeter会内存溢出 第一步:数据准备,根据登录接口需要的参数准备测试数据 例子中,测试的登录接口需要4个参数化数据 ...