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/

posted @ 2021-11-02 16:50  草木物语  阅读(943)  评论(0编辑  收藏  举报