Web Service Response encoding problem
andersm Dec 19, 2007 3:10 PMHi,
I'm having problem with the encoding of java.lang.String in my Web Service response.
Seems like a String in a complex data structure will be returned as ISO-8859-1 instead of UTF-8.
If the WebMethod only returns a String it will be interpreted correctly as UTF-8. Very odd.
I've written a small example made up of one interface:
package se.test.webservice.bean; @javax.jws.WebService @javax.jws.soap.SOAPBinding(style = javax.jws.soap.SOAPBinding.Style.RPC) public interface Text extends java.rmi.Remote { @javax.jws.WebMethod(operationName = "TEST1") String test1(@javax.jws.WebParam(name = "Text1") String x); @javax.jws.WebMethod(operationName = "TEST2") String[] test2(@javax.jws.WebParam(name = "Text2") String x); }
and a stateless session bean:
package se.test.webservice.bean; @javax.ejb.Stateless @javax.jws.WebService(endpointInterface = "se.test.webservice.bean.Text") public class TextBean { public String test1(String x) { return ">" + x + "<"; } public String[] test2(String x) { String[] returnValue = {">" + x + "<", ">" + x + "<"}; return returnValue; } }
The jar is deployed on JBoss AS 4.2.1.GA.
Calling method TEST1, I'm using the Web Service Explorer from within Eclipse, with a character string containing Swedish special characters "üåäöÜÅÄÖ–" (I hope these will come out OK when I submit this post) the response looks fine. But calling method TEST2 with the same string will produce a response that looks like it is UTF-8 characters interpreted as ISO-8859-1: "üåäöÜÅÄ֓". Strange.
This is the produced wsdl:
<definitions name='TextBeanService' targetNamespace='http://bean.webservice.test.se/' xmlns='http://schemas.xmlsoap.org/wsdl/' xmlns:ns1='http://jaxb.dev.java.net/array' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:tns='http://bean.webservice.test.se/' xmlns:xsd='http://www.w3.org/2001/XMLSchema'> <types> <xs:schema targetNamespace='http://jaxb.dev.java.net/array' version='1.0' xmlns:xs='http://www.w3.org/2001/XMLSchema'> <xs:complexType final='#all' name='stringArray'> <xs:sequence> <xs:element maxOccurs='unbounded' minOccurs='0' name='item' nillable='true' type='xs:string' /> </xs:sequence> </xs:complexType> </xs:schema> </types> <message name='Text_TEST2'> <part name='Text2' type='xsd:string'></part> </message> <message name='Text_TEST1'> <part name='Text1' type='xsd:string'></part> </message> <message name='Text_TEST2Response'> <part name='return' type='ns1:stringArray'></part> </message> <message name='Text_TEST1Response'> <part name='return' type='xsd:string'></part> </message> <portType name='Text'> <operation name='TEST1' parameterOrder='Text1'> <input message='tns:Text_TEST1'></input> <output message='tns:Text_TEST1Response'></output> </operation> <operation name='TEST2' parameterOrder='Text2'> <input message='tns:Text_TEST2'></input> <output message='tns:Text_TEST2Response'></output> </operation> </portType> <binding name='TextBinding' type='tns:Text'> <soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http' /> <operation name='TEST1'> <soap:operation soapAction='' /> <input> <soap:body namespace='http://bean.webservice.test.se/' use='literal' /> </input> <output> <soap:body namespace='http://bean.webservice.test.se/' use='literal' /> </output> </operation> <operation name='TEST2'> <soap:operation soapAction='' /> <input> <soap:body namespace='http://bean.webservice.test.se/' use='literal' /> </input> <output> <soap:body namespace='http://bean.webservice.test.se/' use='literal' /> </output> </operation> </binding> <service name='TextBeanService'> <port binding='tns:TextBinding' name='TextBeanPort'> <soap:address location='http://127.0.0.1:8080/TextBeanService/TextBean' /> </port> </service> </definitions>
This is the response from TEST1:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Header /> <env:Body> <ns0:TEST1Response xmlns:ns0="http://bean.webservice.test.se/"> <return>>üåäöÜÅÄÖ<</return> </ns0:TEST1Response> </env:Body> </env:Envelope>
And the response from TEST2:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Header /> <env:Body> <ns0:TEST2Response xmlns:ns0="http://bean.webservice.test.se/"> <return> <item>>üåäöÜÅÄ֓<</item> <item>>üåäöÜÅÄ֓<</item> </return> </ns0:TEST2Response> </env:Body> </env:Envelope>
I hope someone can shed some light on the problem.
I looked at the preview and of course all the characters looked like crap.
I'll try to restore them above but if I fail the correct version of the characters should look like "üåäöÜÅÄÖ" or "üåäöÜÅÄÖ" whatever version looks best.
I wont even try to fix the TEST2-response.
Regards,
Anders