使用SAAJ发送向WebService发送soap之后,返回的消息中文出现乱码
是这样的,我参考http://www.ibm.com/developerworks/cn/xml/x-jaxmsoap/中的示例,进行测试,由于示例中的WebService不能用,我自己用myEclipse中的JAX-WS框架创建了一个简单的WebService——myFirstWS,myFirstWS对外提供一个sayHello(String name)方法,这个方法返回name+",this is my first Jaxws!".
当客户端调用时,当传入的name为中文的时候,在服务器端接受到的参数很正常,不出现乱码,但是返回到客户端的消息中就会出现乱码。具体代码如下:
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
public class SOAPTip {
public static void main(String args[]) {
try {
// First create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
//Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "gb2312");
SOAPPart soapPart = message.getSOAPPart();
//Create objects for the message parts
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
//Populate the body
//Create the main element and namespace
SOAPElement bodyElement = body.addChildElement(envelope.createName("sayHello", "ns1", "http://jaxws.shaw.com/"));
//Add content
String city = "北京";
bodyElement.addChildElement("arg0").addTextNode(city);
//Save the message
message.saveChanges();
//Check the input
System.out.println("\\nREQUEST:\\n");
message.writeTo(System.out);
System.out.println();
//Send the message and get a reply
//Set the destination
String destination = "http://localhost:8080/myFirstWS/HelloJaxwsPort?wsdl";
//Send the message
SOAPMessage reply = connection.call(message, destination);
//Check the output
System.out.println("\\nRESPONSE:\\n");
//Create the transformer
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
//Extract the content of the reply
Source sourceContent = reply.getSOAPPart().getContent();
//Set the output for the transformation
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
System.out.println();
// Close the connection
connection.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
上面的程序传入的name为“北京”,运行之后,客户端接收到如下消息:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:sayHelloResponse xmlns:ns2="http://jaxws.shaw.com/"><return>鍖椾含,this is my first Jaxws!</return></ns2:sayHelloResponse></S:Body></S:Envelope>
请各位帮忙看一下问题出在什么地方?谢谢。
另外,myFirstWS的wsdl和xsd文档如下:
wsdl:
<?xml version="1.0" encoding="UTF-8" ?>
- <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.1-b01-.
-->
- <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.1-b01-.
-->
- <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://jaxws.shaw.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://jaxws.shaw.com/" name="HelloJaxwsService">
- <types>
- <xsd:schema>
<xsd:import namespace="http://jaxws.shaw.com/" schemaLocation="http://localhost:8080/myFirstWS/HelloJaxwsPort?xsd=1" />
</xsd:schema>
</types>
- <message name="add">
<part name="parameters" element="tns:add" />
</message>
- <message name="addResponse">
<part name="parameters" element="tns:addResponse" />
</message>
- <message name="sayHello">
<part name="parameters" element="tns:sayHello" />
</message>
- <message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse" />
</message>
- <portType name="HelloJaxwsDelegate">
- <operation name="add">
<input wsam:Action="http://jaxws.shaw.com/HelloJaxwsDelegate/addRequest" message="tns:add" />
<output wsam:Action="http://jaxws.shaw.com/HelloJaxwsDelegate/addResponse" message="tns:addResponse" />
</operation>
- <operation name="sayHello">
<input wsam:Action="http://jaxws.shaw.com/HelloJaxwsDelegate/sayHelloRequest" message="tns:sayHello" />
<output wsam:Action="http://jaxws.shaw.com/HelloJaxwsDelegate/sayHelloResponse" message="tns:sayHelloResponse" />
</operation>
</portType>
- <binding name="HelloJaxwsPortBinding" type="tns:HelloJaxwsDelegate">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <operation name="add">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" />
</input>
- <output>
<soap:body use="literal" />
</output>
</operation>
- <operation name="sayHello">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" />
</input>
- <output>
<soap:body use="literal" />
</output>
</operation>
</binding>
- <service name="HelloJaxwsService">
- <port name="HelloJaxwsPort" binding="tns:HelloJaxwsPortBinding">
<soap:address location="http://localhost:8080/myFirstWS/HelloJaxwsPort" />
</port>
</service>
</definitions>
xsd:
<?xml version="1.0" encoding="UTF-8" ?>
- <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.1-b01-.
-->
- <xs:schema xmlns:tns="http://jaxws.shaw.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://jaxws.shaw.com/">
<xs:element name="add" type="tns:add" />
<xs:element name="addResponse" type="tns:addResponse" />
<xs:element name="sayHello" type="tns:sayHello" />
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse" />
- <xs:complexType name="sayHello">
- <xs:sequence>
<xs:element name="arg0" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
- <xs:complexType name="sayHelloResponse">
- <xs:sequence>
<xs:element name="return" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
- <xs:complexType name="add">
- <xs:sequence>
<xs:element name="arg0" type="xs:int" />
<xs:element name="arg1" type="xs:int" />
</xs:sequence>
</xs:complexType>
- <xs:complexType name="addResponse">
- <xs:sequence>
<xs:element name="return" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:schema>
[解决办法]
统一字符集,服务器端和客户端都使用统一的字符集UTF-8