基于SAAJ的Web服务----(一)创建和返回SOAP信封

1:首先我们得有一个web服务

         java代码如下:

@WebService(endpointInterface = "demo.hw.server.HelloWorld",  serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {

    public String sayHi() {
        return "Hello " ;
    }


}

省略掉了接口和配置文件,这里用的是cxf发布的服务。


       发布的wsdl如下

  <?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions name="HelloWorld" targetNamespace="http://server.hw.demo/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.hw.demo/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xs:schema elementFormDefault="unqualified" targetNamespace="http://server.hw.demo/" version="1.0" xmlns:tns="http://server.hw.demo/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="sayHi" type="tns:sayHi" /> 
  <xs:element name="sayHiResponse" type="tns:sayHiResponse" /> 
- <xs:complexType name="sayHi">
  <xs:sequence /> 
  </xs:complexType>
- <xs:complexType name="sayHiResponse">
- <xs:sequence>
  <xs:element minOccurs="0" name="return" type="xs:string" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:schema>
  </wsdl:types>
- <wsdl:message name="sayHi">
  <wsdl:part element="tns:sayHi" name="parameters" /> 
  </wsdl:message>
- <wsdl:message name="sayHiResponse">
  <wsdl:part element="tns:sayHiResponse" name="parameters" /> 
  </wsdl:message>
- <wsdl:portType name="HelloWorld">
+ <wsdl:operation name="sayHi">
  <wsdl:input message="tns:sayHi" name="sayHi" /> 
  <wsdl:output message="tns:sayHiResponse" name="sayHiResponse" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="HelloWorldSoapBinding" type="tns:HelloWorld">
  <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
- <wsdl:operation name="sayHi">
  <soap:operation soapAction="" style="document" /> 
- <wsdl:input name="sayHi">
  <soap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output name="sayHiResponse">
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="HelloWorld">
+ <wsdl:port binding="tns:HelloWorldSoapBinding" name="HelloWorldImplPort">
  <soap:address location="http://localhost:8080/test/HelloWorld" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>


2:使用SAAJ创建SOAP访问服务

            代码如下:

public void buildSoapEnv() {
		try {
			URL wsdl = new URL("http://localhost:8080/test/HelloWorld?wsdl");
			String ns = "http://server.hw.demo/";
			String svcName = "HelloWorld";
			QName svcQName = new QName(ns, svcName);
			Service service = Service.create(wsdl, svcQName);
			String portName = "HelloWorldImplPort";
			QName portQName = new QName(ns, portName);
			Dispatch<SOAPMessage> dispatch = service.createDispatch(portQName,SOAPMessage.class, Service.Mode.MESSAGE);
			
			
			
			SOAPMessage soapMsg = MessageFactory.newInstance().createMessage();
			SOAPPart soapPart = soapMsg.getSOAPPart();
			SOAPEnvelope env = soapPart.getEnvelope();
			SOAPBody body = env.getBody();
			String iNs = "http://server.hw.demo/";
			String elementName = "sayHi";
			QName isbnQName = new QName(iNs, elementName);
			body.addBodyElement(isbnQName);
			soapMsg.writeTo(System.out);
			System.out.println();
			System.out.println("---invoke-----");
			SOAPMessage response = dispatch.invoke(soapMsg);
			response.writeTo(System.out);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}


 

                    运行结果,如下:

 

 

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<sayHi xmlns="http://server.hw.demo/"/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

----invoke---

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header/>
<soap:Body>
<ns2:sayHiResponse xmlns:ns2="http://server.hw.demo/">
<return>Hello </return>
</ns2:sayHiResponse>
</soap:Body>
</soap:Envelope>

我们可以看到我们创建的SOAP信封和Web服务返回的SOAP信封
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值