一、基本概念
有代碼優先和契約優先兩種開發webservice的方式,本例介紹契約優先的webservice。
編寫wsdl有三種方式:基于document的wrapper方式,基于document的bare方式,基于rpc方式。本例介紹wraper方式,也是預設方式和推薦方式。wrapper有包起來的意思,将所有對象通過element封裝。
二、編寫步驟
①服務端
1、編寫wsdl
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.example.org/hello/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/xmlschema"
name="helloservice"
targetnamespace="http://www.example.org/hello/">
<wsdl:types>
<xsd:schema targetnamespace="http://www.example.org/hello/">
<xsd:element name="add" type="tns:add"></xsd:element>
<xsd:element name="addresponse" type="tns:addresponse"></xsd:element>
<xsd:element name="licenceinfo" type="tns:licenceinfo"></xsd:element>
<xsd:complextype name="add">
<xsd:sequence>
<xsd:element name="a" type="xsd:int"></xsd:element>
<xsd:element name="b" type="xsd:int"></xsd:element>
</xsd:sequence>
</xsd:complextype>
<xsd:complextype name="addresponse">
<xsd:element name="addresponse" type="xsd:int"></xsd:element>
<xsd:complextype name="licenceinfo">
<xsd:element name="licenceinfo" type="xsd:string"></xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="add">
<wsdl:part name="add" element="tns:add"></wsdl:part>
</wsdl:message>
<wsdl:message name="addresponse">
<wsdl:part name="addresponse" element="tns:addresponse"></wsdl:part>
<wsdl:message name="licenceinfo">
<wsdl:part name="licenceinfo" element="tns:licenceinfo"></wsdl:part>
<wsdl:porttype name="ihelloservice">
<wsdl:operation name="add">
<wsdl:input message="tns:add"></wsdl:input>
<wsdl:output message="tns:addresponse"></wsdl:output>
</wsdl:operation>
</wsdl:porttype>
<wsdl:binding name="helloservicesoap" type="tns:ihelloservice">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:input>
<soap:body use="literal" />
<soap:header use="literal" part="licenceinfo" message="tns:licenceinfo"></soap:header>
</wsdl:input>
<wsdl:output>
</wsdl:output>
</wsdl:binding>
<wsdl:service name="helloservice">
<wsdl:port binding="tns:helloservicesoap" name="helloserviceport">
<soap:address location="http://localhost:8080/hello" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
2、通過wsimport生成java檔案,将生成的java類檔案複制到項目中一般在src下建立mate-inf/wsdl兩層檔案夾,将檔案放進去。
3、删除生成的java檔案,隻保留生成的接口的.java檔案,删除該檔案中不需要的代碼。注意不要拷貝.class檔案進去,因為将來如果要在tomcat裡面部署,多餘的.class檔案會讓程式報錯。
import javax.jws.webmethod;
import javax.jws.webparam;
import javax.jws.webresult;
import javax.jws.webservice;
import javax.xml.bind.annotation.xmlseealso;
import javax.xml.ws.requestwrapper;
import javax.xml.ws.responsewrapper;
/**
* this class was generated by the jax-ws ri.
* jax-ws ri 2.1.1 in jdk 6
* generated source version: 2.1
*
*/
@webservice(name = "hello", targetnamespace = "http://www.example.org/hello/")
public interface ihelloservice {
@webmethod
@webresult(name = "addresponse", targetnamespace = "")
@requestwrapper(localname = "add", targetnamespace = "http://www.example.org/hello/", classname = "org.example.hello.add")
@responsewrapper(localname = "addresponse", targetnamespace = "http://www.example.org/hello/", classname = "org.example.hello.addresponse")
public int add(
@webparam(name = "a", targetnamespace = "")
int a,
@webparam(name = "b", targetnamespace = "")
int b ,
@webparam(name="licenceinfo" , header=true) string licenceinfo);
}
4、編寫helloserviceimpl類
@webservice(endpointinterface = "org.example.hello.hello",
servicename = "helloservice" , wsdllocation = "meta-inf/wsdl/hello.wsdl" ,
targetnamespace = "http://www.example.org/hello/", portname="helloserviceport")
public class helloserviceimpl implements ihelloservice {
public int add(int a, int b, string licenceinfo) {
system.out.println(a+b);
system.out.println("licenceinfo:" + licenceinfo );
return a + b;
5、釋出service
import javax.xml.ws.endpoint;
import org.example.hello.helloserviceimpl;
public class servicetest {
public static void main(string[] args) {
endpoint.publish("http://localhost:8080/mywebservice", new helloserviceimpl());
②編寫用戶端
将wsimort生成的java類添加到用戶端。
1、普通方式通路(無法傳遞頭資訊,當然可以通過另外編寫handler添加頭資訊)
import java.net.url;
import javax.xml.namespace.qname;
import javax.xml.ws.service;
import org.example.hello.hello;
public class client01 {
public static void main(string[] args) throws exception{
url url = new url("http://localhost:8080/mywebservice?wsdl");
string namespace = "http://www.example.org/hello/";
qname sname = new qname(namespace, "helloservice");
service service = service.create(url, sname);
ihelloservice hello = service.getport(ihelloservice.class);
int result = hello.add(1, 100);
system.out.println(result);
// 或直接調用生成的方法(這是另一個例子,可以仿照寫)
// myserviceimplservice mis = new myserviceimplservice();
// imyservice ms = mis.getmyserviceimplport();
// system.out.println(ms.add(29, 3));
2、拼裝soapmessage方式通路 (有頭資訊)
import javax.xml.soap.messagefactory;
import javax.xml.soap.soapbody;
import javax.xml.soap.soapbodyelement;
import javax.xml.soap.soapenvelope;
import javax.xml.soap.soapheader;
import javax.xml.soap.soapmessage;
import javax.xml.ws.dispatch;
public class client02 {
* 帶header的請求
*/
qname sname = new qname(namespace , "helloservice");
qname protname = new qname(namespace, "helloserviceport");
dispatch<soapmessage> dispatch = service.createdispatch(protname,soapmessage.class, service.mode.message);
soapmessage msg = messagefactory.newinstance().createmessage() ;
soapenvelope env = msg.getsoappart().getenvelope() ;
soapbody body = env.getbody() ;
soapheader header = env.getheader() ;
if(header == null) header = env.addheader() ;
// 一定要加ns字首
qname addname = new qname(namespace , "add" , "myprefix");
// 添加body資訊
soapbodyelement bodyele = body.addbodyelement(addname);
bodyele.addchildelement("a").setvalue("1");
bodyele.addchildelement("b").setvalue("2");
// 添加頭資訊
qname headername = new qname(namespace , "licenceinfo");
// 設定頭資訊的值
header.addheaderelement(headername).settextcontent("admin");
// 發送前将soap消息列印出來
msg.writeto(system.out);
system.out.println("----------------relult---------------");
soapmessage resultmsg = dispatch.invoke(msg);
// 将傳回的soap消息列印出來
resultmsg.writeto(system.out) ;
原帖位址:http://blog.csdn.net/is_zhoufeng/article/details/8365723
<a target="_blank" href="http://blog.csdn.net/is_zhoufeng/article/details/8365723"></a>