JAX-WS客户端:gzip响应的UnsupportedMediaException

时间:2013-01-18 16:06:53

标签: java jax-ws gzip

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

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)

read JAX-WS应该支持开箱即用的gzip压缩webservice响应,但看起来并非如此。尽管事实响应包含Content-Type:application / x-gzip header,它会尝试使用默认的编解码器,即application / soap + xml。

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

1 个答案:

答案 0 :(得分:0)

您提到的链接告诉它在使用

发送响应时支持gzip
Content-encoding: gzip

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

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

相反,只需设置包含gzip:

的accept-encoding
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"));
相关问题