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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?