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/

  • 相关阅读:
    iptables允许FTP
    FTP服务添加用户及设置权限
    Python之异步IO&RabbitMQ&Redis
    Python之生产者&、消费者模型
    如何使用Git上传项目代码到github
    11-3 基于cookie和session的登录模块
    11-1 会话控制cookie
    11-2 会话控制session
    10-4 文件的下载
    10-3 文件的上传
  • 原文地址:https://www.cnblogs.com/ooo0/p/15499780.html
Copyright © 2011-2022 走看看