English | 中文
通过Java APT生成http请求代码。基于Apache HttpClient5开发,效率高于OpenFeign和Forest的动态代理实现。
以gradle为例
dependencies {
// 使用gson
implementation 'io.github.yeamy:httpclient-apt-gson:1.0.2'
// 或者使用jackson
//implementation 'io.github.yeamy:httpclient-apt-jackson:1.0.2'
// 或者使用jackson xml
//implementation 'io.github.yeamy:httpclient-apt-jacksonxml:1.0.2'
}
import yeamy.restlite.httpclient.GsonRequestAdapter;
import yeamy.restlite.httpclient.GsonResponseHandler;
@HttpClient(
serializeAdapter = GsonRequestAdapter.class,// 请求body的序列化适配器
responseHandler = GsonResponseHandler.class,// http应答数据处理器
uri = "http://localhost:8080", // 基础uri
maxTryTimes = 1,
header = @Values(name = "user-agent", value = "custom-app/1.0"), // 共用的header
cookie = {@Values(name = "1", value = "2"), // 共用的cookie
@Values(name = "3", value = "4")})
public interface DemoClient {// 必须是interface
}
创建一个模板注解
import yeamy.restlite.httpclient.JacksonRequestAdapter;
import yeamy.restlite.httpclient.JacksonResponseHandler;
@HttpClient(
serializeAdapter = JacksonRequestAdapter.class,
responseHandler = JacksonResponseHandler.class,
maxTryTimes = 1,
uri = "http://localhost:8080",
header = @Values(name = "user-agent", value = "custom-app/1.0"),
cookie = {@Values(name = "1", value = "2"),
@Values(name = "3", value = "4")})
public @interface TemplateClient {
}
为接口添加该模板注解
@TemplateClient
// @HttpClient(maxTryTimes = 2) // 可以再次使用HttpClient覆盖TemplateClient的属性
public interface DemoClient {// 必须是interface
}
public interface DemoClient {
@HttpClientRequest(uri = "/baidu")
Object get();
}
- 用大括号声明变量。
- 为java方法添加同名参数。
public interface DemoClient {
@HttpClientRequest(
uri = "http://localhost:8080/a/{u1}?x={{u2}}",
headerMap = "{m1}",
cookieMap = "{m2}",
header = @Values(name = "h", value = "{h1}"),
cookie = @Values(name = "v", value = "{c1}"),
body = {@PartValues(name = "name", value = "{b1}")}
)
String getPo(String u1, String u2, String c1, String h1, String b1, Map<?, String> m1, Map<String, String> m2);
}
变量 | 属性 | 用法 |
---|---|---|
{u1} | uri | 为uri添加变量,该变量将会被URL编码。 |
{{u2}} | uri | 两个大括号,让URI变量不被编码。 |
{h1} | header | 为header定义个变量值。 |
{c1} | cookie | 为cookie定义变量值。 |
{m1} | headerMap | 多个header变量值,通过Map<>传入。 |
{m2} | cookieMap | 多个cookie变量值,通过Map<>传入。 |
{b1} | body | 定义body变量,该变量将被serializeAdapter序列化。 |
属性包括: serializeAdapter, responseHandler, maxTryTimes, protocol, method.
选择顺序: HttpClientRequest > HttpClient > TemplateClient
当http方法未必定义,或者为空字符串时,使用GET,或者POST(如果请求包含body)。
基础uri: HttpClient的uri 或者 TemplateClient
的uri.
子uri: HttpClientRequest的uri.
完整uri: 基础uri + 子uri.
全部: HttpClientRequest的属性集加上HttpClient的属性集,再加上(如果存在)TemplateClient
的属性集。