0

我编写了 JAX-WS(来自 Sun)客户端,它进行服务调用,期望服务器响应被压缩:

Map<String, List<String>> theHeaders = new HashMap<String, List<String>>();
theHeaders.put("Content-Encoding", Collections.singletonList("gzip"));
theHeaders.put("Accept", Collections.singletonList("application/x-gzip"));
theHeaders.put("Accept-Encoding", Collections.singletonList("gzip, deflate"));
theHeaders.put("Content-Type", Collections.singletonList("application/x-gzip"));
((BindingProvider) client).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, theHeaders);

根据 Fiddler 的说法,响应是 HTTP 200 (Ok),soap 响应是 gzip 压缩的。不过,我收到以下错误:

com.sun.xml.ws.server.UnsupportedMediaException: Unsupported Content-Type: application/x-gzip Supported ones are: [application/soap+xml]
at com.sun.xml.ws.encoding.StreamSOAPCodec.decode(StreamSOAPCodec.java:322)
at com.sun.xml.ws.encoding.StreamSOAP12Codec.decode(StreamSOAP12Codec.java:107)
at com.sun.xml.ws.encoding.StreamSOAPCodec.decode(StreamSOAPCodec.java:156)
at com.sun.xml.ws.encoding.SOAPBindingCodec.decode(SOAPBindingCodec.java:312)
at com.sun.xml.ws.transport.http.client.HttpTransportPipe.createResponsePacket(HttpTransportPipe.java:295)

读到JAX-WS 应该支持开箱即用的 gzipped webservice 响应,但看起来它不支持。尽管事实响应包含 Content-Type: application/x-gzip 标头,但它尝试使用适用于 application/soap+xml 的默认编解码器。

有没有办法让它使用另一个编解码器,用于gzip?有这样的编解码器吗?

4

1 回答 1

0

您提到的链接告诉它在发送响应时支持 gzip

Content-encoding: gzip

标头,除了标准的内容类型标头。如果服务器以 响应Content-Type: application/x-gzip,则它是一个不同的标头,并且即使 gzip 本身受支持,它似乎也不受支持。我认为你不应该设置这个:

theHeaders.put("Accept", Collections.singletonList("application/x-gzip"));

相反,只需设置接受编码,包括 gzip:

theHeaders.put("Accept", Collections.singletonList("application/soap+xml"));
theHeaders.put("Content-Type", Collections.singletonList("application/soap+xml"));
theHeaders.put("Accept-Encoding", Collections.singletonList("gzip"));
theHeaders.put("Content-Encoding", Collections.singletonList("gzip"));
于 2013-01-18T16:13:23.423 回答