Enable gzip compression in CXF client

ぐ巨炮叔叔 提交于 2020-01-01 09:18:07

问题


I'm trying to make my client use gzip. I have the GZip Feature enabled in the server. It seems that the client it's not sending the correct header:

POST /api/v1/data HTTP/1.1
Content-Type: text/xml; charset=UTF-8
Accept: */*
SOAPAction: ""
User-Agent: Apache CXF 2.6.2
Cache-Control: no-cache
Pragma: no-cache
Host: localhost:8001
Connection: keep-alive
Content-Length: 539

Here's the code where I create the client:

 private static final QName SERVICE_NAME = new QName(
            "http://xxx/", "IData");
    private static final QName PORT_NAME = new QName(
            "http://xxx/", "IDataPort");
    IData port;

    public void initPort() {
        Service service = Service.create(SERVICE_NAME);
        // Endpoint Address
        String endpointAddress = ClientUtil.getUrl()
                + "data";

        // Add a port to the Service
        service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING,
                endpointAddress);

        port = service.getPort(IData.class);
    }

The IData interface implements has the GZip Annotation:

@WebService
@GZIP
public interface IData ....

回答1:


Solution:

After a revision, this is what you need:

Client client = ClientProxy.getClient(port);
client.getInInterceptors().add(new GZIPInInterceptor());
client.getOutInterceptors().add(new GZIPOutInterceptor());

After that it worked.




回答2:


When only gzip'in the response from the server and not the the request from the client, then you need to add the header and the GZIPInInterceptor like to following:

// add accept-encoding header
Map<String, Object> requestHeaders = new HashMap<>();
requestHeaders.put("Accept-Encoding", new ArrayList<>(Arrays.asList("gzip")));
((BindingProvider)service).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, requestHeaders);

// encode response from server
client.getInInterceptors().add(new GZIPInInterceptor());



回答3:


The easiest way to enable gzip:

List<Feature> features = Arrays.asList(new GZIPFeature())
final WebClient webClient = WebClient.create(uri, null, features, null);

GZipFeature will automatically add "in interceptor", "out interceptor" and "out fault interceptor".




回答4:


As I understand from http://fusesource.com/docs/esb/4.4/cxf_jaxws/JavaFirst-AnnotateCxf-Compress.html

"GZIP is a negotiated enhancement. That is, an initial request from a client will not be gzipped, but an Accept header will be added and, if the server supports GZIP compression, the response will be gzipped and any subsequent requests will be also."

Check if the web service accepts Gzip, and check only requests after the first request.




回答5:


more detailed answer to above mentioned answer

Client client = ClientProxy.getClient(port);

//this line to send compressed(gzip) request to server
client.getOutInterceptors().add(new GZIPOutInterceptor());

//this in to uncompress server response at client side
client.getInInterceptors().add(new GZIPInInterceptor());


来源:https://stackoverflow.com/questions/12523365/enable-gzip-compression-in-cxf-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!