如何在RESTEasy客户端框架中设置HTTP标头? [英] How to set HTTP header in RESTEasy client framework?

查看:683
本文介绍了如何在RESTEasy客户端框架中设置HTTP标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RESTEasy(一个JAX-RS实现)有一个很好的客户端框架,例如:

RESTEasy (a JAX-RS implementation) has a nice client framework, eg:

RegisterBuiltin.register(ResteasyProviderFactory.getInstance());

SimpleClient client = ProxyFactory.create(SimpleClient.class, "http://localhost:8081");
client.putBasic("hello world");

如何设置HTTP标头?

How do you set HTTP headers?

澄清:

jkeeler 是一个很好的方法,但我想在ProxyFactory级别设置HTTP标头,我不想将标头传递给客户对象。有什么想法?

The solution proposed by jkeeler is a good approach, but I want to set HTTP headers on ProxyFactory level and I don't want to pass headers to the client object. Any ideas?

推荐答案

我找到了一个解决方案:

I have found a solution:

import org.apache.commons.httpclient.HttpClient;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.client.core.executors.ApacheHttpClientExecutor;
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
import org.jboss.resteasy.spi.ResteasyProviderFactory;

RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
HttpClient httpClient = new HttpClient();
ApacheHttpClientExecutor executor = new ApacheHttpClientExecutor(httpClient) {
    @Override
    public ClientResponse execute(ClientRequest request) throws Exception {
        request.header("X-My-Header", "value");
        return super.execute(request);
    }           
};

SimpleClient client = ProxyFactory.create(SimpleClient.class, "http://localhost:8081", executor);
client.putBasic("hello world");

这篇关于如何在RESTEasy客户端框架中设置HTTP标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆