HttpClient-APT基于Apache HttpClient5开发,不同于Openfeign和Forest的动态代理,该项目通过Java APT生成实现类的代码。
只需要创建接口interface
,为接口添加注解配置HTTP请求信息,APT会在编译时自动生成实现代码,类名默认为接口名加Impl后缀,也可以自定义类名。
开源地址:https://github.com/Yeamy/httpclient-apt
1、添加依赖
此处以gradle为例
// 该项目基于 Apache HttpClient5开发
dependencies {
// 使用gson处理json
implementation 'io.github.yeamy:httpclient-apt-gson:1.0.3'
// 或者使用jackson处理json
//implementation 'io.github.yeamy:httpclient-apt-jackson:1.0.3'
// 或者使用jackson处理xml
//implementation 'io.github.yeamy:httpclient-apt-jacksonxml:1.0.3'
// 启用注解处理器
annotationProcessor 'io.github.yeamy:httpclient-apt-gson:1.0.3'
}
2、为interface添加HttpClient注解
使用@HttpClient
注解将共有的http请求参数添加到类。
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 {
}
使用模板:只需要为接口interface
添加模板注解即可
@TemplateClient
// @HttpClient(maxTryTimes = 2) // 可以再次使用HttpClient覆盖TemplateClient的属性
public interface DemoClient {// 必须是interface
}
2、使用HttpClientRequest注解创建请求方法
public interface DemoClient {
@HttpClientRequest(uri = "/baidu")
Object get();
}
3、声明变量
- 用大括号声明变量。
- 为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序列化。 |
4、属性是如何被选择的
相同的属性将被覆盖
属性包括: serializeAdapter, responseHandler, maxTryTimes, protocol, method.
选择顺序: HttpClientRequest > HttpClient > 模板注解
默认HTTP方法:
当http方法未必定义,或者为空字符串时,使用GET,或者POST(如果请求包含body)。
完整的URI由基础URI加上子URI组成:
基础uri: HttpClient的uri 或者 TemplateClient
的uri.
子uri: HttpClientRequest的uri.
完整uri: 基础uri + 子uri.
headers和cookies:
全部: HttpClientRequest的属性集加上HttpClient的属性集,再加上(如果存在的话)TemplateClient
的属性集。
网友评论