【问题标题】:Enable gzip compression in CXF client在 CXF 客户端中启用 gzip 压缩
【发布时间】:2012-09-21 02:08:28
【问题描述】:

我正在尝试让我的客户使用 gzip。我在服务器中启用了 GZip 功能。似乎客户端没有发送正确的标头:

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

这是我创建客户端的代码:

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

IData 接口实现了 GZip Annotation:

@WebService
@GZIP
public interface IData ....

【问题讨论】:

  • 标头是否来自某些网络分析器,例如 tcpmon?
  • 我正在使用 Grinder 代理连接并获取流量转储。

标签: java web-services cxf


【解决方案1】:

解决方案:

修改后,这是你需要的:

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

之后就成功了。

【讨论】:

  • 对于 JAX-RS RESTful 客户端:Client client = WebClient.client(port); ClientConfiguration config = WebClient.getConfig(client); config.getInInterceptors().add(new GZIPInInterceptor()); config.getOutInterceptors().add(new GZIPOutInterceptor());
【解决方案2】:

据我了解http://fusesource.com/docs/esb/4.4/cxf_jaxws/JavaFirst-AnnotateCxf-Compress.html

"GZIP 是一种协商的增强。也就是说,来自 客户端不会被压缩,但会添加一个 Accept 标头,如果 服务器支持 GZIP 压缩,响应将被 gzip 压缩并 任何后续请求也将是。”

检查网络服务是否接受 Gzip,并且只检查第一个请求之后的请求。

【讨论】:

  • 我需要配置客户端,使其发送标头以触发您提到的协商。不过谢谢!
【解决方案3】:

当只有 gzip'in 来自服务器的响应而不是来自客户端的请求时,您需要添加标头和 GZIPInInterceptor 如下所示:

// 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());

【讨论】:

    【解决方案4】:

    启用 gzip 的最简单方法:

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

    GZipFeature 会自动添加“in 拦截器”、“out 拦截器”和“out 故障拦截器”。

    【讨论】:

      【解决方案5】:

      对上述答案的更详细的回答

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

      【讨论】:

      • 为什么它被否决了..?提供更多说明..?
      猜你喜欢
      • 2010-10-23
      • 2023-01-25
      • 2013-12-29
      • 2014-09-26
      • 2013-05-05
      • 2011-10-11
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      相关资源
      最近更新 更多