FeignJava HTTP 客户端库
Feign 使得 Java HTTP 客户端编写更方便。
Feign 灵感来源于 Retrofit, JAXRS-2.0 和 WebSocket,Feign 最初是为了降低统一绑定 Denominator 到 HTTP APIs 的复杂度,不管是否是 Restful 。
Feign 使用Jersey和CXF之类的工具为ReST或SOAP服务编写Java客户端。此外,Feign允许用户在诸如Apache HC之类的http库之上编写自己的代码。Feign通过可定制的解码器和错误处理功能,以最小的开销和代码将代码连接到http API,这些错误和错误处理可以写入任何基于文本的http API。
基础使用:
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
}
static class Contributor {
String login;
int contributions;
}
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("netflix", "feign");
for (Contributor contributor : contributors) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
}
}
评论
