openfeign+retronfit http 访问
一个简单记录
maven 依赖
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>11.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-gson</artifactId>
<version>11.0</version>
</dependency>
openfeign参考代码
private static void openFeign() {
GitHub github = Feign.builder()
.decoder(new GsonDecoder())
.target(GitHub.class, "https://api.github.com");
List<Contributor> contributors = github.contributors("OpenFeign", "feign");
for (Contributor contributor : contributors) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
}
}
package com.dalong;
import feign.Param;
import feign.RequestLine;
import java.util.List;
/**
*
* github api
*/
public interface GitHub {
/**
* contributors 获取
* @param owner
* @param repo
* @return
*/
@RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
/**
* issue 请求获取
* @param issue
* @param owner
* @param repo
*/
@RequestLine("POST /repos/{owner}/{repo}/issues")
void createIssue(Issue issue, @Param("owner") String owner, @Param("repo") String repo);
}
retrofit2
package com.dalong;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import java.util.List;
/**
*
* github api
*/
public interface GitHub2 {
/**
* contributors 获取
* @param owner
* @param repo
* @return
*/
@GET("/repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributors(@Path("owner") String owner, @Path("repo") String repo);
/**
* issue 请求获取
* @param issue
* @param owner
* @param repo
*/
@POST("/repos/{owner}/{repo}/issues")
void createIssue(@Body Issue issue, @Path("owner") String owner, @Path("repo") String repo);
}
private static void retrofit() throws IOException {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
// Fetch and print a list of the contributors to this library.
GitHub2 gitHub2 = retrofit.create(GitHub2.class);
Call<List<Contributor>> contributors2 = gitHub2.contributors("OpenFeign", "feign");
for (Contributor contributor : contributors2.execute().body()) {
System.out.println("github2");
System.out.println(contributor.login + " (" + contributor.contributions + ")");
}
}
参考资料
https://github.com/square/retrofit
https://github.com/OpenFeign/feign
https://mvnrepository.com/artifact/com.squareup.retrofit2
https://mvnrepository.com/artifact/io.github.openfeign
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2019-11-06 Checking Types Against the Real World in TypeScript
2019-11-06 nexus pip proxy config
2018-11-06 ballerina 学习 三十一 扩展开发(二)
2018-11-06 ballerina 学习 三十 扩展开发(一)
2017-11-06 Webpack-dashboard 简单使用
2014-11-06 rpc介绍