forest:bean-id: config0# 在spring上下文中bean的id, 默认值为forestConfigurationbackend: okhttp3# 后端HTTP API: okhttp3,默认为okhttp3,也可以改为httpclientmax-connections: 1000# 连接池最大连接数,默认值为500max-route-connections: 500 # 每个路由的最大连接数,默认值为500timeout: 3000# 请求超时时间,单位为毫秒, 默认值为3000connect-timeout: 3000# 连接超时时间,单位为毫秒, 默认值为2000retry-count: 1# 请求失败后重试次数,默认为0次不重试ssl-protocol: SSLv3# 单向验证的HTTPS的默认SSL协议,默认为SSLv3logEnabled: true# 打开或关闭日志,默认为truelog-request: true# 打开/关闭Forest请求日志(默认为 true)log-response-status: true# 打开/关闭Forest响应状态日志(默认为 true)log-response-content: true# 打开/关闭Forest响应内容日志(默认为 false)
配置Bean ID
Forest 允许在 yaml 文件中配置 Bean Id,它对应着ForestConfiguration对象在 Spring 上下文中的 Bean 名称 。
forest:bean-id: config0# 在spring上下文中bean的id,默认值为forestConfiguration
然后便可以在 Spring 中通过 Bean 的名称引用到它
@Resource(name = "config0")private ForestConfiguration config0;
4.1.3 构建接口在 Forest 依赖加入好之后,就可以构建 HTTP 请求的接口了,在 Forest 中,所有的 HTTP 请求信息都要绑定到某一个接口的方法上,不需要编写具体的代码去发送请求 。
请求发送方通过调用事先定义好 HTTP 请求信息的接口方法,自动去执行 HTTP 发送请求的过程,其具体发送请求信息就是该方法对应绑定的 HTTP 请求信息 。
public interface MyClient {/*** 获取用户所有设备信息*/@Post(url = "https://yunlong.farm.xiaomaiot.com/v6/device_chunk/all",headers = {"token: ${token}","Content-Type:application/json"})String getDevice(@DataVariable("token") String token);}
4.1.4 HTTP Method(1)POST方式
public interface MyClient {/*** 通过 @Request 注解的 type 参数指定 HTTP 请求的方式 。*/@Request(url = "http://localhost:8080/hello",type = "POST")String simplePost();/*** 使用 @Post 注解,可以去掉 type = "POST" 这行属性*/@Post("http://localhost:8080/hello")String simplePost();/*** 使用 @PostRequest 注解,和上面效果等价*/@PostRequest("http://localhost:8080/hello")String simplePost();}
(2)GET请求
// GET请求@Request(url = "http://localhost:8080/hello",type = "get")String simpleGet();
(3)PUT请求
// PUT请求@Request(url = "http://localhost:8080/hello",type = "put")String simplePut();
(4)HEAD请求
// HEAD请求@Request(url = "http://localhost:8080/hello",type = "head")String simpleHead();
(5)Options请求
// Options请求@Request(url = "http://localhost:8080/hello",type = "options")String simpleOptions();
(6)Delete请求
// Delete请求@Request(url = "http://localhost:8080/hello",type = "delete")String simpleDelete();
注:
- @Get和@GetRequest两个注解的效果是等价的,@Post和@PostRequest、@Put和@PutRequest等注解也是同理
- HEAD请求类型没有对应的@Head注解,只有@HeadRequest注解,原因是容易和@Header注解混淆
基本URL设置方法只要在url属性中填入完整的请求地址即可 。
除此之外,也可以从外部动态传入URL:
/** * 整个完整的URL都通过 @DataVariable 注解修饰的参数动态传入 */@Request("${myURL}")String send1(@DataVariable("myURL") String myURL);/** * 通过参数转入的值值作为URL的一部分 */@Request("http://${myURL}/abc")String send2(@DataVariable("myURL") String myURL);
4.1.6 HTTP Header(1)headers属性文章插图
该接口调用后所实际产生的 HTTP 请求如下:
GET http://localhost:8080/hello/userHEADER:Accept-Charset: utf-8Content-Type: text/plain
(2)数据绑定文章插图
这段调用所实际产生的 HTTP 请求如下:
GET http://localhost:8080/hello/userHEADER:Accept-Charset: gbkContent-Type: text/plain
(3)@Header注解/** * 使用 @Header 注解将参数绑定到请求头上 * @Header 注解的 value 指为请求头的名称,参数值为请求头的值 * @Header("Accept") String accept将字符串类型参数绑定到请求头 Accept 上 * @Header("accessToken") String accessToken将字符串类型参数绑定到请求头 accessToken 上 */@Post("http://localhost:8080/hello/user?username=foo")void postUser(@Header("Accept") String accept, @Header("accessToken") String accessToken);
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 七里河|老者用这款“神饵”在七里河钓鱼,让他成为整条河最靓的“仔”
- 无刘海短发图片
- 安热沙|这款防晒霜真的好,夏天别人晒黑了我却没黑,没有安耐晒小金瓶
- 护肤品|护肤品真不是越贵越好,这款国产护肤品,价格便宜还超好用!
- 马云花18亿打造的这款车,销量惨不忍睹,你觉得这车怎么样?
- 抖音十大神曲排行榜
- 葛根鸡米汤怎么做?
- 洗发水|“清扬、阿道夫”用腻了?试试这款国货洗发水,虽平价却真心好用
- 帽子|李宁推出的这款服装是特色还是忘本?
- 只需4分钟轻松搞定减肥早餐