How to set HTTP header in RESTEasy client framework?

前端 未结 4 729
栀梦
栀梦 2021-01-31 11:19

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

RegisterBuiltin.register(ResteasyProviderFactory.getInstance());

SimpleClient client = Proxy         


        
4条回答
  •  Happy的楠姐
    2021-01-31 12:00

    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");
    

提交回复
热议问题