java类:
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import org.w3c.dom.Document;
public class Receive {
/**
* @param args
*/
@SuppressWarnings("unused")
public static void main(String[] args) throws Exception {
String ns = "http://axisversion.sample";
String wsdlUrl = "http://127.0.0.1:8080/axis2/services/Version?wsdl";
//1、创建服务(Service)
URL url = new URL(wsdlUrl);
QName sname = new QName(ns,"Version");
Service service = Service.create(url,sname);
//2、创建Dispatch
Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns,"VersionHttpSoap11Endpoint"),SOAPMessage.class,Service.Mode.MESSAGE);
//3、创建SOAPMessage
SOAPMessage msg = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL).createMessage();
SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
SOAPBody body = envelope.getBody();
//4、创建QName来指定消息中传递数据
QName ename = new QName(ns,"getVersion","axis");//<nn:add xmlns="xx"/>
SOAPBodyElement ele = body.addBodyElement(ename);
// 传递参数
// ele.addChildElement("a").setValue("22");
// ele.addChildElement("b").setValue("33");
msg.writeTo(System.out);
System.out.println("\n invoking.....");
//5、通过Dispatch传递消息,会返回响应消息
SOAPMessage response = dispatch.invoke(msg);
response.writeTo(System.out);
System.out.println();
//6、响应消息处理,将响应的消息转换为dom对象
Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
String str = doc.getElementsByTagName("ns:return").item(0).getTextContent();
System.out.println(str);
}
}
wsdl文件:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://axisversion.sample" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://axisversion.sample">
<wsdl:documentation>Version</wsdl:documentation>
<wsdl:types>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://axisversion.sample">
<xs:element name="VersionException">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="VersionException" nillable="true" type="ns:Exception"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Exception">
<xs:sequence>
<xs:element minOccurs="0" name="Message" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="getVersion">
<xs:complexType>
<xs:sequence/>
</xs:complexType>
</xs:element>
<xs:element name="getVersionResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</wsdl:types>
<wsdl:message name="getVersionRequest">
<wsdl:part name="parameters" element="ns:getVersion"/>
</wsdl:message>
<wsdl:message name="getVersionResponse">
<wsdl:part name="parameters" element="ns:getVersionResponse"/>
</wsdl:message>
<wsdl:message name="VersionException">
<wsdl:part name="parameters" element="ns:VersionException"/>
</wsdl:message>
<wsdl:portType name="VersionPortType">
<wsdl:operation name="getVersion">
<wsdl:input message="ns:getVersionRequest" wsaw:Action="urn:getVersion"/>
<wsdl:output message="ns:getVersionResponse" wsaw:Action="urn:getVersionResponse"/>
<wsdl:fault message="ns:VersionException" name="VersionException" wsaw:Action="urn:getVersionVersionException"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="VersionSoap11Binding" type="ns:VersionPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="getVersion">
<soap:operation soapAction="urn:getVersion" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
<wsdl:fault name="VersionException">
<soap:fault use="literal" name="VersionException"/>
</wsdl:fault>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="VersionSoap12Binding" type="ns:VersionPortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="getVersion">
<soap12:operation soapAction="urn:getVersion" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
<wsdl:fault name="VersionException">
<soap12:fault use="literal" name="VersionException"/>
</wsdl:fault>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="VersionHttpBinding" type="ns:VersionPortType">
<http:binding verb="POST"/>
<wsdl:operation name="getVersion">
<http:operation location="getVersion"/>
<wsdl:input>
<mime:content type="application/xml" part="parameters"/>
</wsdl:input>
<wsdl:output>
<mime:content type="application/xml" part="parameters"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Version">
<wsdl:port name="VersionHttpSoap11Endpoint" binding="ns:VersionSoap11Binding">
<soap:address location="http://127.0.0.1:8080/axis2/services/Version.VersionHttpSoap11Endpoint/"/>
</wsdl:port>
<wsdl:port name="VersionHttpSoap12Endpoint" binding="ns:VersionSoap12Binding">
<soap12:address location="http://127.0.0.1:8080/axis2/services/Version.VersionHttpSoap12Endpoint/"/>
</wsdl:port>
<wsdl:port name="VersionHttpEndpoint" binding="ns:VersionHttpBinding">
<http:address location="http://127.0.0.1:8080/axis2/services/Version.VersionHttpEndpoint/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
控制台输出:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><axis:getVersion xmlns:axis="http://axisversion.sample"/></SOAP-ENV:Body></SOAP-ENV:Envelope>
invoking.....
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header/><soapenv:Body><ns:getVersionResponse xmlns:ns="http://axisversion.sample"><ns:return>Hi - the Axis2 version is 1.6.2</ns:return></ns:getVersionResponse></soapenv:Body></soapenv:Envelope>
Hi - the Axis2 version is 1.6.2