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 ...
随机推荐
- 几个常见的漏洞(xss攻击 cookie未清除 nginx信息泄露)与处理方法
项目在安全检查中发现很多问题,要求整改,其中就有最常见的xss攻击 漏洞描述 渗透测试人员检测到网站筛选框均存在反射型跨站脚本攻击,例如: "><script>alert( ...
- 使用Emacs来编程
使用Emacs来编程 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} code ...
- python基础【第八篇】
day06笔记 1.小数据池 is 与 ==的区别 is :判断两边的内存地址是否相同 ==:判断两边的值是否相同 python中的驻留机制: 数字: -5 ~ 256 字符串: 3.6 乘法 ...
- Java菜鸟笔记
System.out.println( ); 会在输出完毕后自动换行 System.out.print( ); 在输出完毕后不会自动换行 MyEclipse/Eclipse快捷键: 定位到某一行, ...
- sudo 出现unable to resolve host hostname 解决方法
Linux 环境,我的电脑叫枝桠(机器的hostname), 每次执行sudo 就出现这个警告讯息: sudo: unable to resolve host 枝桠 直接修改 /etc/hosts 的 ...
- java全栈商业小程序开发
此次开发只为学习和巩固,第一次学习开发 一.开发前需要了解: 开发框架MVVM.痛点.开源工具.VUE前端框架.微信支付模块.uni-app前端框架.小程序申请.开发工具下载.编写测试小程序.小程序结 ...
- Python 基础 4-1 字典入门
引言 字典 是Python 内置的一种数据结构,它便于语义化表达一些结构数据,字典是开发中常用的一种数据结构 字典介绍 字典使用花括号 {} 或 dict 来创建,字典是可以嵌套使用的 字典是成对出现 ...
- Codeforces Round #556 CF1149D Abandoning Roads
这道题并不简单,要得出几个结论之后才可以做.首先就是根据Kruskal求最小生成树的过程,短边是首选的,那么对于这道题也是,我们先做一次直选短边的最小生成树这样会形成多个联通块,这些联通块内部由短边相 ...
- ubuntu 安装pip并修改为阿里云pip源
0.sudo su1.安装pipapt-get install python-pip python-dev build-essentialpip install --upgrade pip pip i ...
- koa 中间件 koa-art-template 的使用
例子 const Koa = require('koa'); const render =require('koa-art-template'); const path= require('path' ...