Question

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

Was it helpful?

Solution

Instead of setting the protocol headers directly like that, use CXF's GZIPOutInterceptor to handle that.

Either that or reset the PROTOCOL headers for each request. When set like that, the headers map gets updated as the request goes through the chain. In this case, the soapaction gets set. This then gets resent on the second request.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top