zoukankan      html  css  js  c++  java
  • 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));
        }
    }
  • 相关阅读:
    JSON的使用总结
    pc端分页插件的使用
    简单修改选择文件样式
    H5中的本地存储
    H5中的 meta 标签及 移动页面单位
    1001. A+B Format (20)
    查看mysql的注册表路径
    win10 64位安装mysql
    [POLITICS] S Korea lawmakers vote to impeach leader
    5-17 Hashing (25分)
  • 原文地址:https://www.cnblogs.com/wanlige/p/14735583.html
Copyright © 2011-2022 走看看