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:2.0.1'
// or with jackson
//implementation 'io.github.yeamy:httpclient-apt-jackson:2.0.1'
// or with jackson xml
//implementation 'io.github.yeamy:httpclient-apt-jacksonxml:2.0.1'
}
import yeamy.restlite.httpclient.GsonRequestHandler;
import yeamy.restlite.httpclient.GsonResponseHandler;
@HttpClient(uri = "http://localhost:8080", // set the base uri
requestBodyHandler = GsonRequestHandler.class,// set request serialize adapter
responseHandler = GsonResponseHandler.class,// set respone handler
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
}
public interface DemoClient {// interface with @HttpClient
@HttpClientRequest(uri = "/hello")
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 create httpclient Entity. |
create an annotation with HttpClient
import yeamy.restlite.httpclient.JacksonRequestHandler;
import yeamy.restlite.httpclient.JacksonResponseHandler;
@HttpClient(
requestBodyHandler = JacksonRequestHandler.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
}
attributes: requestBodyHandler, 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).
Full url: HttpClientRequest.uri() with http scheme, or base uri + sub uri.
base uri: using template annotation
.uri() as the first half url if not exist HttpClient nor HttpClient.uri() is empty.
sub uri: using HttpClientRequest.uri() as the second half url.
total: HttpClientRequest's values + HttpClient's values + TemplateClient
's values.