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

posted on   荣锋亮  阅读(801)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.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介绍

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示