随笔 - 65  文章 - 0  评论 - 3  阅读 - 10万

Retrofit:后端发送HTTP请求

引入Maven依赖:

<dependency>
     <groupId>com.github.lianjiatech</groupId>
     <artifactId>retrofit-spring-boot-starter</artifactId>
     <version>2.2.5</version>
</dependency>

定义HTTP接口:

复制代码
import com.alibaba.fastjson.JSONObject;
import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient;
import retrofit2.http.*;
import java.util.Map;

@RetrofitClient(baseUrl = "https://api.doctorxiong.club")
public interface httpApi {

    @GET("/v1/fund?code=202015,007339")
    Map<String,Object> httpGetTest();

    @POST("/v1/fund/rank")
    Map<String,Object> httpPostTest(@Body JSONObject jsonObject);
    
}
复制代码

将HTTP接口注入Service中使用:

复制代码
@Service
public class testServiceImpl {
    @Autowired
    private httpApi httpApi;

    @Scheduled(cron = "*/5 * * * * ?")
    public void TestTask() {
        JSONObject jsonObject = new JSONObject();
        List<Object> list = new ArrayList<>();
        list.add("zs");
        jsonObject.put("fundType",list);
        jsonObject.put("sort","z");
        List<Object> list1 = new ArrayList<>();
        list1.add("80000248");
        jsonObject.put("fundCompany",list1);
        jsonObject.put("pageIndex",1);
        jsonObject.put("pageSize",10);
        System.out.println(jsonObject);
        Map<String, Object> getMap = httpApi.httpGetTest();
        System.out.println(getMap);
        Map<String, Object> postMap = httpApi.httpPostTest(jsonObject);
        System.out.println(postMap);
    }

}
复制代码

可使用的HTTP网址:https://www.doctorxiong.club/api/#api-Fund-fund

Retrofit 的GitHub官网:https://github.com/LianjiaTech/retrofit-spring-boot-starter

Retrofit 的官网文档:https://square.github.io/retrofit/#introduction

 

posted on   陌生街中吹起褪色故梦  阅读(383)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 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

点击右上角即可分享
微信分享提示