1

I´m using CXF to consume a WebService and, as the responses are quite large, I´m requesting with a gzip "Accept-Encoding" and using GZIPInInterceptor to handle the gziped response. Also my WSDL is very large (360kb) and it takes a long time(+10 seconds) to create the stub, because it has to read and parse the WSDL, so I´m creating the stub once and reusing it.

The problem is, whenever I try to use two different methods the second request gives me an error saying it is expecting the previous request.

To illustrate my problem I created a simple example with this public WebService:

http://www.webservicex.net/BibleWebservice.asmx?WSDL

Without the GZip compression it works fine:

BibleWebserviceSoap bibleService = new BibleWebservice().getBibleWebserviceSoap();

String title = bibleService.getBookTitles();
response.getWriter().write(title);
String johnResponse = bibleService.getBibleWordsbyKeyWord("John");
response.getWriter().write(johnResponse);

I´m able to receive both responses. Enabling Gzip compression:

BibleWebserviceSoap bibleService = new BibleWebservice().getBibleWebserviceSoap();

//GZIP compression on bibleService
Client client = ClientProxy.getClient(bibleService);

client.getInInterceptors().add(new GZIPInInterceptor());
client.getInFaultInterceptors().add(new GZIPInInterceptor());

// Creating HTTP headers
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Accept-Encoding", Arrays.asList("gzip"));

// Add HTTP headers to the web service request
client.getRequestContext().put(Message.PROTOCOL_HEADERS, headers);

String title = bibleService.getBookTitles();
response.getWriter().write(title);

String johnResponse = bibleService.getBibleWordsbyKeyWord("John");
response.getWriter().write(johnResponse);

When I try to receive the second response I´m getting this exception: org.apache.cxf.interceptor.Fault: Unexpected wrapper element {http://www.webserviceX.NET}GetBookTitlesResponse found. Expected {http://www.webserviceX.NET}GetBibleWordsbyKeyWordResponse.

On my real application I´m getting an exception with the request: org.apache.cxf.binding.soap.SoapFault: OperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'GetAvailabilityRequest' and namespace 'http://schemas.navitaire.com/WebServices/ServiceContracts/BookingService'. Found node type 'Element' with name 'ns4:PriceItineraryRequest' and namespace 'http://schemas.navitaire.com/WebServices/ServiceContracts/BookingService'

My sample project can be downloaded here: http://www.sendspace.com/file/plt0m4

Thank you

4

1 回答 1

2

不要像那样直接设置协议头,而是使用 CXF 的 GZIPOutInterceptor 来处理它。

要么为每个请求重置 PROTOCOL 标头。当这样设置时,标头映射会随着请求通过链而更新。在这种情况下,soapaction 被设置。然后在第二个请求上重新发送。

于 2013-09-05T04:53:01.930 回答