Java 使用ApacheHTTP客户端库将默认值前置到HTTP请求路径
我试图在使用ApacheHTTP客户端库配置客户端时设置默认的基本URI路径。然而,我找不到关于如何进行这项工作的任何信息 本质上,我希望做的是在默认情况下将基本路径注入/前置到给定的请求路径上。因此,如果请求路径类似于“/employees/1024”,那么我希望在路径前面加上“/api/v1”,以便在执行请求时得到“/api/v1/employees/1024”的URI路径 在构建HttpClient对象时,我希望能够做到这一点。我肯定可以在我的堆栈中进一步实现这个逻辑,但如果可能的话,我希望避免这种情况Java 使用ApacheHTTP客户端库将默认值前置到HTTP请求路径,java,apache,configuration,apache-httpclient-4.x,Java,Apache,Configuration,Apache Httpclient 4.x,我试图在使用ApacheHTTP客户端库配置客户端时设置默认的基本URI路径。然而,我找不到关于如何进行这项工作的任何信息 本质上,我希望做的是在默认情况下将基本路径注入/前置到给定的请求路径上。因此,如果请求路径类似于“/employees/1024”,那么我希望在路径前面加上“/api/v1”,以便在执行请求时得到“/api/v1/employees/1024”的URI路径 在构建HttpClient对象时,我希望能够做到这一点。我肯定可以在我的堆栈中进一步实现这个逻辑,但如果可能的话,我希
有人知道在HttpClient配置期间是否可以设置此选项吗?(通过重写可设置对象方法或其他方式)我从未找到我问题的直接答案。我的解决方案是扩展
CloseableHttpClient
抽象类,提供要附加的路径字符串以及CloseableHttpClient
(用于合成)的具体实例到构造函数。然后,我使用HttpRequestWrapper
类将路径字符串预先添加到覆盖方法中给定的HttpRequest
对象的URL上
以下是我的实现示例:
class PureHttpClient extends CloseableHttpClient {
private final CloseableHttpClient client;
private final String service;
PureHttpClient(CloseableHttpClient client, String service) {
this.client = client;
this.service = service;
}
@Override
public void close() throws IOException {
if (client != null)
client.close();
}
private HttpUriRequest appendService(HttpRequest request, String service) throws ClientProtocolException {
HttpRequestWrapper wrappedRequest = HttpRequestWrapper.wrap(request);
try {
URI uri = wrappedRequest.getURI();
URI newUri = new URIBuilder(uri)
.setPath(service + uri.getPath())
.build();
wrappedRequest.setURI(newUri);
} catch (URISyntaxException e) {
throw new ClientProtocolException(e.getMessage(), e);
}
return wrappedRequest;
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public HttpParams getParams() {
return client.getParams();
}
@Override
public ClientConnectionManager getConnectionManager() {
return client.getConnectionManager();
}
@Override
public CloseableHttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException {
return client.execute(appendService(request, service));
}
@Override
public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException, ClientProtocolException {
return client.execute(appendService(request, service), context);
}
@Override
public CloseableHttpResponse execute(HttpHost target, HttpRequest request) throws IOException, ClientProtocolException {
return client.execute(target, appendService(request, service));
}
@Override
public CloseableHttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException {
return client.execute(target, appendService(request, service), context);
}
@Override
public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler) throws IOException, ClientProtocolException {
return client.execute(appendService(request, service), responseHandler);
}
@Override
public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException, ClientProtocolException {
return client.execute(appendService(request, service), responseHandler);
}
@Override
public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler) throws IOException, ClientProtocolException {
return client.execute(target, appendService(request, service), responseHandler);
}
@Override
public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException, ClientProtocolException {
return client.execute(target, appendService(request, service), responseHandler);
}
@Override
protected CloseableHttpResponse doExecute(HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException {
return this.execute(target, request, context);
}
}
类PureHttpClient扩展了CloseableHttpClient{
私有最终可关闭HttpClient客户端;
私人最终字符串服务;
PureHttpClient(CloseableHttpClient客户端,字符串服务){
this.client=client;
服务=服务;
}
@凌驾
public void close()引发IOException{
如果(客户端!=null)
client.close();
}
私有HttpUriRequest appendService(HttpRequest请求,字符串服务)引发ClientProtocolException{
HttpRequestWrappedRequest=HttpRequestWrapper.wrap(请求);
试一试{
URI=wrappedRequest.getURI();
URI newUri=新的URIBuilder(URI)
.setPath(服务+uri.getPath())
.build();
setURI(newUri);
}捕获(URISyntaxException e){
抛出新的ClientProtocolException(e.getMessage(),e);
}
返回wrappedRequest;
}
@凌驾
公共int hashCode(){
返回super.hashCode();
}
@凌驾
公共HttpParams getParams(){
返回client.getParams();
}
@凌驾
公共客户端连接管理器getConnectionManager(){
返回client.getConnectionManager();
}
@凌驾
public CloseableHttpResponse执行(HttpUriRequest请求)引发IOException、ClientProtocolException{
返回client.execute(appendService(请求,服务));
}
@凌驾
public CloseableHttpResponse执行(HttpUriRequest请求,HttpContext上下文)抛出IOException、ClientProtocolException{
返回client.execute(appendService(请求、服务)、context);
}
@凌驾
public CloseableHttpResponse执行(HttpHost目标,HttpRequest请求)抛出IOException、ClientProtocolException{
返回client.execute(target,appendService(request,service));
}
@凌驾
public CloseableHttpResponse执行(HttpHost目标、HttpRequest请求、HttpContext上下文)抛出IOException、ClientProtocolException{
返回client.execute(目标、附加服务(请求、服务)、上下文);
}
@凌驾
public T execute(HttpUriRequest request,responsehandler)我当前的解决方案涉及扩展CloseableHttpClient抽象类,并在执行方法重写实现期间使用CloseableHttpClient子类实例的组合来预先设置基路径。