
v1.5.3 本版发布了,本次版本更新新增了较多功能,其中有许多重大更新。
1. 新增Forest快捷接口
以前版本使用 Forest,必须先定义一个 interface 接口类,这种形式可以满足大多数情况的场景。 但若想快速访问一个url可能显得不合时宜。 所以本次更新新增了快捷接口,不用再从定义接口开始了。
它大概长这个样子:
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 | String str = Forest.get( "/" ).executeAsString();
MyResult myResult = Forest
.post( "/" )
.execute(MyResult.class);
Result<List<User>> userList = Forest
.post( "/" )
.execute( new TypeReferenceList<Result<List<User>>>() {});
Map<String, Object> map = Forest.post( "/" )
.backend( "okhttp3" )
.contentTypeJson()
.host( "127.0.0.1" )
.port(8080)
.addBody( "a" , 1)
.addBody( "b" , 2)
.maxRetryCount(3)
.onSuccess((data, req, res) -> { log.info( "success!" ); })
.onError((ex, req, res) -> { log.info( "error!" ); })
.successWhen((req, res) -> res.noException() && res.statusOk())
.executeAsMap();
|
2. 请求成功条件/重试条件
@Success 注解
先要定义 SuccessWhen 接口的实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class TestSuccessWhen implements SuccessWhen {
@Override
public boolean successWhen(ForestRequest req, ForestResponse res) {
return res.noException() && res.statusOk() && res.statusCode() != 203;
}
}
|
以上就是Forest v1.5.3 正式版本发布的介绍,希望对大家有所帮助。更多精彩内容分享:头条