Forest Forest
💒 首页
  • v1.5.30
  • v1.5.28
  • 🎄 ForestX
🌰 案例
💖 支持
🛫 更新记录
🧢 开发团队
⚒️ 参与贡献
  • MaxKey - 业界领先的身份管理和认证产品 (opens new window)
  • Snowy - 国内首个国密前后端分离快速开发平台 (opens new window)
  • Eoapi - 一个开源、可拓展的 API 工具平台 (opens new window)
  • Gitee (opens new window)
  • Github (opens new window)
💒 首页
  • v1.5.30
  • v1.5.28
  • 🎄 ForestX
🌰 案例
💖 支持
🛫 更新记录
🧢 开发团队
⚒️ 参与贡献
  • MaxKey - 业界领先的身份管理和认证产品 (opens new window)
  • Snowy - 国内首个国密前后端分离快速开发平台 (opens new window)
  • Eoapi - 一个开源、可拓展的 API 工具平台 (opens new window)
  • Gitee (opens new window)
  • Github (opens new window)
  • 序言

    • 🎁 新手介绍
    • 📖 文档
    • 🌰 使用案例
    • 🕵️‍ 关于作者
    • 👨‍🎓 贡献者列表
  • 入门

    • 🎬 安装配置说明
    • 🏹 Springboot环境安装
    • 📐 Springboot环境配置
    • 🎯 Springboot环境使用
    • 🏹 Springboot3环境安装
    • 📐 Springboot3环境配置
    • 🎯 Springboot3环境使用
    • 🏹 Spring环境安装
    • 📐 Spring环境配置
    • 🎯 Spring环境使用
    • 🏹 Solon环境安装
    • 📐 Solon环境配置
    • 🎯 Solon环境使用
    • 🏹 原生Java环境安装
    • 📐 原生Java环境配置
    • 🎯 原生Java环境使用
    • 🧬 编程式接口
  • 配置项

    • 👜 Springboot环境配置项
    • 👝 Spring环境配置项
      • 👜 Solon环境配置项
      • 🎒 原生Java环境配置项
      • 📚 配置优先级/作用域
    • 声明式接口

      • 🧱 构建接口
      • 🍀 请求方法
      • 🚚 请求地址
      • 🎈 URL 参数
      • 🍭 请求头
      • 👔 请求体
      • 🍮 后端框架
      • 🧁 接口注解
      • 📬 接收数据
      • 🍛 数据转换
      • 🍓 成功/失败条件
      • 🍌 重试机制
      • 🥂 重定向
      • 🍔 Gzip解压
      • 🎂 日志管理
      • ⚽ 回调函数
      • 🍟 异步请求
      • 🛡️ HTTPS
      • 🍪 使用Cookie
      • 🛸 使用代理
      • 🍉 上传下载
      • 🚑 异常处理
    • 编程式接口

      • 请求API

        • 🚀 请求对象
        • 🚢 请求属性
        • ✨ 执行请求
        • 🎊 后端框架
        • 🎪 请求类型
        • 🔮 请求地址
        • 🧀 URL 参数
        • 🚅 请求头
        • 🚋 请求体
        • ⚓ 回调函数
        • 🚁 异步请求
        • 🥯 Cookie
        • 🍜 成功/失败条件
        • 🌶️ 重试机制
        • ⛵ 重定向
        • 🛰️ 请求代理
      • 响应API

        • 🌠 响应对象
        • ✒️ 读取数据
        • 🦋 响应状态码
        • 🏥 响应错误处理
        • 🎧 响应头
        • 🥞 Cookie
    • 模板表达式

      • 🍬 Hello World
      • 🍹 配置属性引用
      • 🍖 变量引用
      • 🥃 动态变量绑定
      • 🥗 参数序号引用
      • 🍍 引用对象属性
      • 🥝 调用对象方法
    • 高级特性

      • 🥪 拦截器
      • 🍏 自定义注解
      • 🍇 组合注解
      • 🥑 自定义转换器
    • v1.5.30文档
    • 配置项
    公子骏
    2022-07-01
    目录

    👝 Spring环境配置项

    若您的项目依赖的是Spring,而非Spring Boot,或者使用xml方式进行Spring Bean的配置,那么您可以通过Spring xml的方式定义配置。

    # 依赖

    在进行Spring方式配置前,需要先确保您的项目在Maven中除了forest-core和spring外,还依赖forest-spring包

     <dependency>
         <groupId>com.dtflys.forest</groupId>
         <artifactId>forest-spring</artifactId>
         <version>1.5.30</version>
     </dependency>
    
    1
    2
    3
    4
    5

    # 配置 XML SCEHEMA

    打开spring的上下文配置文件,在beans开头定义的属性中加入Forest的Schema

    xmlns:forest="http://forest.dtflyx.com/schema/forest" 
       ...
    xsi:schemaLocation=" ...
    http://forest.dtflyx.com/schema/forest
    http://forest.dtflyx.com/schema/forest/forest-spring.xsd 
    ..."
    
    1
    2
    3
    4
    5
    6

    加入完成后类似如下效果

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:forest="http://forest.dtflyx.com/schema/forest"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://forest.dtflyx.com/schema/forest
           http://forest.dtflyx.com/schema/forest/forest-spring.xsd">
        
       ...
    
    </beans>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

    # 添加Forest基本配置的定义

    <!-- Forest 全局配置 -->
    <!-- id 在spring上下文中bean的id, 默认值为forestConfiguration -->
    <!-- backend 后端HTTP API: okhttp3 -->
    <!-- timeout 请求超时时间,单位为毫秒, 默认值为3000 -->
    <!-- connectTimeout 连接超时时间,单位为毫秒, 默认值为2000 -->
    <!-- retryCount 请求失败后重试次数,默认为0次不重试 -->
    <!-- retryer 重试器类 -->
    <!-- sslProtocol 单向验证的HTTPS的默认SSL协议,默认为SSLv3 -->
    <!-- maxConnections 每个路由的最大连接数,默认为500 -->
    <!-- maxRouteConnections 每个路由的最大连接数,默认为500 -->
    <!-- maxRequestQueueSize [自v1.5.22版本起可用] 最大请求等待队列大小 -->
    <!-- maxAsyncThreadSize [自v1.5.21版本起可用] 最大异步线程数 -->
    <!-- maxAsyncQueueSize [自v1.5.22版本起可用] 最大异步线程池队列大小 -->
    <!-- logEnabled 打开或关闭日志总开关,默认为true -->
    <!-- logRequest 打开/关闭Forest请求日志(默认为 true) -->
    <!-- logResponseStatus 打开/关闭Forest响应状态日志(默认为 true) -->
    <!-- logResponseContent 打开/关闭Forest响应内容日志(默认为 false) -->
    <forest:configuration
        id="config0"
        backend="httpclient"
        timeout="30000"
        connectTimeout="10000"
        retryCount="3"
        retryer="com.dtflys.forest.retryer.NoneRetryer"
        charset="UTF-8"
        sslProtocol="SSLv3"
        maxConnections="500"
        maxRouteConnections="500"
        maxRequestQueueSize="100"
        maxAsyncThreadSize="256"
        maxAsyncQueueSize="128"
        logEnabled="true"
        logRequest="false"
        logResponseStatus="false"
        logResponseContent="true">
    
       <!-- forest变量定义 开始 -->
       <forest:var name="baseUrl" value="http://www.xxx.com"/>
       <forest:var name="x" value="0"/>
       <forest:var name="y" value="1"/>
       <!-- forest变量定义 结束 -->
    
        <!-- SSL KeyStore定义 开始 -->
        <forest:ssl-keystore id="keystore1" file="test.keystore" keystorePass="123456" certPass="123456"/>
        <forest:ssl-keystore id="keystore2" file="test2.keystore" keystorePass="foo" certPass="bar"/>
        <!-- SSL KeyStore定义 结束 -->
        
        <!-- Forest转换器定义 开始 -->
        <!-- 设置JSON转换器 -->
        <forest:converter dataType="json" class="com.dtflys.forest.converter.json.ForestGsonConverter">
            <forest:parameter name="dateFormat" value="yyyy/MM/dd hh:mm:ss"/>
        </forest:converter>
    
        <!-- 设置XML转换器 -->
        <forest:converter dataType="xml" class="com.dtflys.forest.converter.xml.ForestJaxbConverter">
        </forest:converter>
        <!-- Forest转换器定义 结束 -->
        
    </forest:configuration>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    1. 使用forest:configuration标签创建在Spring中的ForestConfiguration Bean

    2. 使用forest:var标签定义变量

      注意:变量的作用域为该ForestConfiguration之下,所有跟这个配置对象绑定的Client都能访问到其下的变量,而别的ForestConfiguration下定义的变量不能访问。

    # 创建Client Bean

    创建Client Bean有两种方式

    1. 通过forest:client标签创建单个Client Bean
       <forest:client id="siteAClient" configuration="config0" class="com.xxx.client.SiteAClient"/>
    
    1
    1. 通过forest:scan标签制定back-package的方式批量创建Client Bean
        <forest:scan configuration="config0" base-package="com.xxx.client"/>
    
    1
    帮助我们改善此文档 (opens new window)
    上次更新: 2023/03/14, 11:24:19
    👜 Springboot环境配置项
    👜 Solon环境配置项

    ← 👜 Springboot环境配置项 👜 Solon环境配置项→

    Theme by Vdoing | Copyright © 2016-2023 公子骏 | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式