public class RequestInvoker extends Object implements InvocationHandler, HttpClientConfig, HttpOperations
UTF_8| 限定符和类型 | 方法和说明 |
|---|---|
void |
addHeader(String name,
String value)
添加全局header
|
void |
addInterceptor(Interceptor interceptor)
添加拦截器
|
void |
addParameter(String name,
Object value)
添加全局请求参数
|
static <T> T |
create(Class<T> proxyInterface)
使用
Thread.currentThread().getContextClassLoader()作为类加载器创建请求代理 |
static <T> T |
create(Class<T> proxyInterface,
ClassLoader classLoader)
创建请求代理
|
<T> T |
exchange(String url,
HttpMethod method,
BodyType bodyType,
ResponseType responseType,
Type responseEntityType,
Map<String,String> httpHeaders,
Map<String,Object> uriVariables,
Object requestBody)
自定义执行http请求
|
Object |
invoke(Object proxy,
Method method,
Object[] args) |
void |
removeInterceptors(Class<? extends Interceptor> interceptorClass)
批量移除拦截器
|
void |
removeInterceptors(Interceptor interceptor)
批量移除拦截器
|
void |
setBasicAuth(String username,
String password)
设置http basic auth
|
void |
setCharset(Charset charset)
设置编码
|
void |
setHost(String host)
设置替换协议(http/https),主机(host),端口(port)的uri
|
void |
setHttpClient(org.apache.http.impl.client.CloseableHttpClient httpClient)
使用自定义的http client
|
void |
setProxy(String host,
int port,
String scheme)
设置代理
|
void |
setThreadPool(ExecutorService threadPool)
使用自定义的线程池
|
void |
setTimeout(int socketTimeout,
int connectTimeout)
设置套接字超时时间和建立请求超时时间
|
void |
setUserAgent(String userAgent)
设置user-agent
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitexchange, exchange, getForEntity, getForEntity, getForEntity, getForJsonEntity, getForJsonEntity, postForEntity, postForEntity, postForEntity, postForJsonEntity, postForJsonEntitypublic static <T> T create(Class<T> proxyInterface)
Thread.currentThread().getContextClassLoader()作为类加载器创建请求代理proxyInterface - 被代理的接口public static <T> T create(Class<T> proxyInterface, ClassLoader classLoader)
proxyInterface - 被代理的接口classLoader - 类加载器public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
invoke 在接口中 InvocationHandlerThrowablepublic void addInterceptor(Interceptor interceptor)
HttpClientConfigaddInterceptor 在接口中 HttpClientConfigpublic void removeInterceptors(Interceptor interceptor) throws NoSuchElementException
HttpClientConfigremoveInterceptors 在接口中 HttpClientConfigNoSuchElementExceptionpublic void removeInterceptors(Class<? extends Interceptor> interceptorClass) throws NoSuchElementException
HttpClientConfigremoveInterceptors 在接口中 HttpClientConfigNoSuchElementExceptionpublic void setCharset(Charset charset)
HttpClientConfigsetCharset 在接口中 HttpClientConfigpublic void setUserAgent(String userAgent)
HttpClientConfigsetUserAgent 在接口中 HttpClientConfigpublic void addHeader(String name, String value)
HttpClientConfigaddHeader 在接口中 HttpClientConfigpublic void addParameter(String name, Object value)
HttpClientConfigaddParameter 在接口中 HttpClientConfigname - 参数名称value - 参数值public void setHost(String host)
HttpClientConfigsetHost 在接口中 HttpClientConfighost - 参数必须是http(s)://host:port,如果是80,443则可以省略public void setProxy(String host, int port, String scheme)
HttpClientConfigsetProxy 在接口中 HttpClientConfighost - 主机地址port - 端口scheme - 协议:如httppublic void setTimeout(int socketTimeout,
int connectTimeout)
HttpClientConfigsetTimeout 在接口中 HttpClientConfigsocketTimeout - 设置套接字的超时时间(等待数据响应的超时时间),即两个数据包之间可以间隔的最大时间。如果为0则永不超时,如果为负值则使用系统设置,默认为-1connectTimeout - 设置建立连接所允许的超时时间。如果为0则永不超时,如果为负值则使用系统设置,默认为-1public void setHttpClient(org.apache.http.impl.client.CloseableHttpClient httpClient)
HttpClientConfigsetHttpClient 在接口中 HttpClientConfigpublic void setThreadPool(ExecutorService threadPool)
HttpClientConfigsetThreadPool 在接口中 HttpClientConfigpublic void setBasicAuth(String username, String password)
HttpClientConfigsetBasicAuth 在接口中 HttpClientConfigusername - 用户名password - 密码public <T> T exchange(@Nonnull String url, @Nonnull HttpMethod method, @Nonnull BodyType bodyType, @Nonnull ResponseType responseType, @Nonnull Type responseEntityType, @Nullable Map<String,String> httpHeaders, @Nullable Map<String,Object> uriVariables, @Nullable Object requestBody)
HttpOperationsexchange 在接口中 HttpOperationsurl - 请求地址。如请求地址为https://example.com/foo/bar,则url应为/foo/barmethod - 请求方法。如GET, POST等bodyType - 请求body类型responseType - response body类型responseEntityType - 返回实体类型 TypeReferencehttpHeaders - 请求headeruriVariables - 查询参数requestBody - 请求bodyresponseEntityType指定的返回值Copyright © 2019. All rights reserved.