zoukankan      html  css  js  c++  java
  • 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/

  • 相关阅读:
    redis 五大数据类型
    redis 对 key 的操作
    redis 零散知识
    redis helloworld
    CentOS下内存使用率查看
    不需要客户端插件PHP也能实现单点登录
    mysql 删匿名帐户
    mysql5.6默认情况下内存占用太大
    PHPExcel生成或读取excel文件
    通过SMTP发送邮件的Python代码
  • 原文地址:https://www.cnblogs.com/ooo0/p/15499780.html
Copyright © 2011-2022 走看看