• SpringBoot整合Forest实现调用第三方HTTP协议接口


    一、背景

    因为业务关系,要和许多不同第三方公司进行对接。这些服务商都提供基于http的api。但是每家公司提供api具体细节差别很大。有的基于RESTFUL规范,有的基于传统的http规范;有的需要再header里放置签名,有的需要SSL的双向认证,有的只需要SSL的单向认证;有的以JSON 方式进行序列化,有的以XML方式进行序列化。类似于这样细节的差别太多了。

    于是了解到了Forest

    Forest是什么?

    Forest是一个高层的、极简的轻量级 HTTP调用API框架,让Java发送HTTP/HTTPS请求不再难。它比OkHttp和HttpClient更高层,比Feign更轻量,是封装调用第三方restful api client接口的好帮手。

    相比于直接使用Httpclient我们不再写一大堆重复的代码了,而是像调用本地方法一样去发送HTTP请求。

    二、SpringBoot整合Forest实现调用第三方接口

    1、pom.xml中引入依赖

    <!-- Forest -->
    <dependency>
      <groupId>com.dtflys.forest</groupId>
      <artifactId>spring-boot-starter-forest</artifactId>
      <version>1.4.0</version>
    </dependency>

    2、application.yml中相关配置

    # ========================== ↓↓↓↓↓↓ forest配置 ↓↓↓↓↓↓ ==========================
    forest:
      bean-id: config0 # 在spring上下文中bean的id, 默认值为forestConfiguration
      backend: okhttp3 # 后端HTTP API: okhttp3 【支持`okhttp3`/`httpclient`】
      max-connections: 1000 # 连接池最大连接数,默认值为500
      max-route-connections: 500 # 每个路由的最大连接数,默认值为500
      timeout: 3000 # 请求超时时间,单位为毫秒, 默认值为3000
      connect-timeout: 3000 # 连接超时时间,单位为毫秒, 默认值为2000
      retry-count: 0 # 请求失败后重试次数,默认为0次不重试
      ssl-protocol: SSLv3 # 单向验证的HTTPS的默认SSL协议,默认为SSLv3
      logEnabled: true # 打开或关闭日志,默认为true
    

    3、配置扫描接口

    在启动类上加上@ForestScan注解,并在basePackages属性中填写远程接口所在的包名

    // forest扫描远程接口所在的包名
    @ForestScan(basePackages = "com.wanli.demo.client")
    @SpringBootApplication
    public class DemoApplication {
    
    public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
    }
    
    }

    4、构建请求接口

    public interface MyClient {
    
        /**
         * 本地测试接口
         */
        @Get(url = "http://127.0.0.1:80/demo/index")
        String index();
    
        @Get(url = "http://127.0.0.1:80/demo/hello?msg=${msg}")
        String hello(@DataVariable("msg") String msg);
    
        /**
         * 高德地图API
         */
        @Get(url = "http://ditu.amap.com/service/regeo?longitude=${longitude}&latitude=${latitude}")
        Map getLocation(@DataVariable("longitude") String longitude, @DataVariable("latitude") String latitude);
    
    }

    5、测试

    @Slf4j
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class DemoTest {
    
        @Autowired
        private MyClient myClient;
    
        @Test
        public void testForest() throws Exception {
            // 调用接口
            String index = myClient.index();
            log.info("index: 【{}】", index);
            
            String hello = myClient.hello("测试...");
            log.info("hello: 【{}】", hello);
    
            Map location = myClient.getLocation("121.475078", "31.223577");
            log.info("location: 【{}】", location.toString());
             System.out.println(JSON.toJSONString(location));
        }
    }
  • 相关阅读:
    matlab安装YaHei Consolas Hybrid字体
    Matlab各种拟合
    正态分布
    JQuery插件:遮罩+数据加载中。。。(特点:遮你想遮,罩你想罩)
    【调侃】IOC前世今生
    使用jquery的 uploadify,在谷歌浏览器上总会崩溃的解决方法
    源代码管理工具TFS2013安装与使用
    C# 给枚举定义DescriptionAttribute,把枚举转换为键值对
    Bootstrap 中文官网
    时间 时间戳 转换
  • 原文地址:https://www.cnblogs.com/wanlige/p/14735583.html
Copyright © 2020-2023  润新知