forest HTTP調用API框架


Forest是一個高層的、極簡的輕量級HTTP調用API框架。
相比於直接使用Httpclient不再用寫一大堆重復的代碼了,而是像調用本地方法一樣去發送HTTP請求。

 

添加Maven依賴

<dependency>
    <groupId>com.dtflys.forest</groupId>
    <artifactId>forest-spring-boot-starter</artifactId>
    <version>1.5.11</version>
</dependency>

 

創建一個interface

以高德地圖API為例 

package com.xc.xcspringboot.client;

import com.dtflys.forest.annotation.Get;

import java.util.Map;

public interface AmapClient {

    /**
     * 聰明的你一定看出來了@Get注解代表該方法專做GET請求
     * 在url中的${0}代表引用第一個參數,${1}引用第二個參數
     */
    @Get("https://ditu.amap.com/service/regeo?longitude=${0}&latitude=${1}")
    Map getLocation(String longitude, String latitude);
}

 

掃描接口

在Spring Boot的配置類或者啟動類上加上@ForestScan注解,並在basePackages屬性里填上遠程接口的所在的包名

@SpringBootApplication
@ForestScan(basePackages = "com.xc.xcspringboot.client")
public class XcSpringbootApplication {

 

 

調用接口

    // 注入接口實例
    @Resource
    private AmapClient amapClient;

    @RequestMapping(value = "/getLocation", method = RequestMethod.GET)
    public Object getLocation() {
        // 調用接口
        Map result = amapClient.getLocation("121.475078", "31.223577");
        log.info(result.toString());
        return result;
    }

 

 

 

gitee:

https://gitee.com/dromara/forest

 

官方文檔:

http://forest.dtflyx.com/docs/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM