java - 使用 Apache http 客户端库将默认值添加到 HTTP 请求路径

标签 java apache configuration apache-httpclient-4.x

我正在尝试使用 Apache http 客户端库在客户端配置时设置默认基本 URI 路径。但是,我找不到任何有关如何进行此操作的信息。

本质上,我想要做的是默认情况下将基本路径注入(inject)/添加到给定的请求路径上。因此,如果请求路径类似于“/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);
    }
}

关于java - 使用 Apache http 客户端库将默认值添加到 HTTP 请求路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43328284/

相关文章:

java - 无法使用 Jackson 解码 LocalDate 和 LocalTime 类

apache - 如何在 Ubuntu 中安装 apache2

configuration - Gradle:如何复制文件但保持相同的构建调用?

java - 表未映射的 Hibernate 消息

java - 使用字符串数组列表长度作为整数

java - 如何使某些东西与数组和集合兼容?

linux - apache 2.2 中的虚拟主机问题

Apache mod_rewrite : force www only if not in localhost

iOS:用于移动设备管理的配置文件

php - 在服务器上配置 php/MVC