English | 中文
Generate simple http clients with Java APT. Base on Apache HttpClient5, more efficient than the dynamic proxy (such as: OpenFeign and Forest).
Take gradle as an example
dependencies {
// with gson
implementation 'io.github.yeamy:httpclient-apt-gson:1.0.2'
// or with jackson
//implementation 'io.github.yeamy:httpclient-apt-jackson:1.0.2'
// or with 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,// set request serialize adapter
responseHandler = GsonResponseHandler.class,// set respone handler
uri = "http://localhost:8080", // set the base uri
maxTryTimes = 1,
header = @Values(name = "user-agent", value = "custom-app/1.0"), // add common header
cookie = {@Values(name = "1", value = "2"), // add common cookie
@Values(name = "3", value = "4")})
public interface DemoClient {// must be an interface
}
create an annotation with HttpClient
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 {
}
add template for the interface
@TemplateClient
// @HttpClient(maxTryTimes = 2) // can also override attributes of TemplateClient
public interface DemoClient {// must be an interface
}
public interface DemoClient {
@HttpClientRequest(uri = "/baidu")
Object get();
}
- declared parameters with brackets in annotation.
- add java method parameters with same name.
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);
}
variable | Attribute | Usage |
---|---|---|
{u1} | uri | define a parameter for uri, the param will be encoded. |
{{u2}} | uri | double brackets keep the parameter without url encoded (uri only). |
{h1} | header | define a parameter for header value. |
{c1} | cookie | define a parameter for cookie value. |
{m1} | headerMap | define header parameters in a Map<> |
{m2} | cookieMap | define cookie parameters in a Map<> |
{b1} | body | define a body parameter, the body will be serialized by the adapter. |
attributes: serializeAdapter, responseHandler, maxTryTimes, protocol, method.
Selection order: HttpClientRequest > HttpClient > TemplateClient
if http method not defined(empty string), will using "GET" or "POST" (if the request contains body).
base uri: HttpClient's (or TemplateClient
's) uri.
sub uri: HttpClientRequest's uri.
Complete uri: base uri + sub uri.
total: HttpClientRequest's values + HttpClient's values + TemplateClient
's values.