详细介绍WSDL文档
2015-11-10 19:43
267 查看
首先如何生成一个wsdl文档,通过以下的程序暴露一个wsdl文档供大家用
第一步:创建一个接口,该接口有二个函数,一个简单一些,另一个复杂一些。
package yy;
import java.util.List;
import javax.jws.WebService;
import yy.domain.Cat;
import yy.domain.User;
@WebService
public interface HelloWorld {
public String sayHi(String name);
public List<Cat> getCatsByUser(User user);
}
第二步:创建一个实现类,去实现该接口:
package yy.impl;
import java.util.Date;
import java.util.List;
import javax.jws.WebService;
import service.UserService;
import service.impl.UserServiceImpl;
import yy.HelloWorld;
import yy.domain.Cat;
import yy.domain.User;
@WebService(endpointInterface="yy.HelloWorld",serviceName="HelloWorldWS")
public class HelloWorldWS implements HelloWorld{
@Override
public String sayHi(String name) {
return name +" 你好"+"现在的时间是"+new Date();
}
@Override
public List<Cat> getCatsByUser(User user) {
//zai shijixiangmuzhong ,Web Service 自己并不会去实现业务逻辑,
//而去调用已有的业务逻辑
UserService service=new UserServiceImpl();
return service.getCatsByUser(user);
}
}
package yy.domain;
public class Cat {
private Integer id;
private String name;
private String color;
public Cat() {
}
public Cat(Integer id, String name, String color) {
super();
this.id = id;
this.name = name;
this.color = color;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
package yy.domain;
public class User {
private Integer id;
private String name;
private String pass;
private String address;
public User() {
}
public User(Integer id, String name, String pass, String address) {
super();
this.id = id;
this.name = name;
this.pass = pass;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((pass == null) ? 0 : pass.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (pass == null) {
if (other.pass != null)
return false;
} else if (!pass.equals(other.pass))
return false;
return true;
}
}
package service;
import java.util.List;
import yy.domain.*;
public interface UserService {
public List<Cat> getCatsByUser(User user);
}
package service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import service.UserService;
import yy.domain.Cat;
import yy.domain.User;
public class UserServiceImpl implements UserService{
static Map<User,List<Cat>> catDb=new HashMap<User,List<Cat>>();
static{
List<Cat> catList1=new ArrayList<Cat>();
catList1.add(new Cat(1,"garefkds","blcak"));
catList1.add(new Cat(2,"fds","bldascak"));
catDb.put(new User(1,"sun","2233","dad"), catList1);
List<Cat> catList2=new ArrayList<Cat>();
catList2.add(new Cat(3,"garefkds","blcak"));
catList2.add(new Cat(4,"fds","bldascak"));
catDb.put(new User(2,"sun","2233","dad"), catList2);
}
@Override
public List<Cat> getCatsByUser(User user) {
return catDb.get(user);
}
}
第三部:创建一个发布类
package lee;
import javax.xml.ws.Endpoint;
public class ServerMain {
public static void main(String[] args){
HelloWorld hello=new HelloWorldWS();
//发布WebService
Endpoint.publish("http://IP/webservie" ,hello);
}
}
然后访问http://IP/webservice?wsdl 就可以看到以下的WSDL文档了。
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.yy/" x
mlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://yy/" name="HelloWorldWS" targetNamespace="http://impl.yy/">
<wsdl:import location="http://202.118.212.80/webservie?wsdl=HelloWorld.wsdl" namespace="http://yy/"></wsdl:import>
<wsdl:binding name="HelloWorldWSSoapBinding" type="ns1:HelloWorld">...</wsdl:binding>
<wsdl:service name="HelloWorldWS">
<wsdl:port binding="tns:HelloWorldWSSoapBinding" name="HelloWorldWSPort">...</wsdl:port>
</wsdl:service>
</wsdl:definitions>
该文档主要包括二部分:
第一部是就是<wsdl:import />代表Web Service的接口的信息
第二部分就是<wsdl:binding></wsdl:binding><wsdl:service></wsdl:service>
详细介绍第一部分:
<wsdl:import location="http://202.118.212.80/webservie?wsdl=HelloWorld.wsdl" namespace="http://yy/"></wsdl:import>
其中location代表的就是发出这个wsdl文档的位置 namespace代表在/yy这个命名空间下。
接下来我们详细的介绍一下 这个接口类中有什么信息 就是http://202.118.212.80/webservie?wsdl=HelloWorld.wsdl
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://yy/" name="HelloWorld" targetNamespace="http://yy/">
<wsdl:types>...</wsdl:types>
<wsdl:message name="getCatsByUserResponse">...</wsdl:message>
<wsdl:message name="sayHiResponse">...</wsdl:message>
<wsdl:message name="getCatsByUser">...</wsdl:message>
<wsdl:message name="sayHi">...</wsdl:message>
<wsdl:portType name="HelloWorld">...</wsdl:portType>
</wsdl:definitions>
WebService的接口类中包含主要的三个标签
----<wsdl:portType name="HelloWorld">...</wsdl:portType>
这个portType代表是哪一个类,它的子元素代表该类有几个方法,每一个方法的输入消息,以及输出消息
<wsdl:portType>
<wsdl:operation name="getCatsByUser">
<wsdl:input message="ns1:getCatsByUser" name="getCatsByUser"></wsdl:input>
<wsdl:output message="ns1:getCatsByUserResponse" name="getCatsByUserResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="sayHi">
<wsdl:input message="ns1:sayHi" name="sayHi"></wsdl:input>
<wsdl:output message="ns1:sayHiResponse" name="sayHiResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
从上面这段我们可以看出,这个接口的名字叫HelloWorld 该类有二个方法,第一个方法叫sayHi 第二个方法叫getCatsByUser
那么我们就会想知道,每一个方法的参数是什么,返回值是什么?接下来我们要看这个操作对应的输入消息是什么
我们拿sayHi这个操作来说:<wsdl:input message="ns1:sayHi" name="sayHi"></wsdl:input>
因此我们需要找到消息的名字是sayHi的消息,即<wsdl:message name="sayHi">...</wsdl:message>
我们详细的解析一下这个:
<wsdl:message name="sayHi">
<wsdl:part element="ns1:sayHi" name="parameters"></wsdl:part>
</wsdl:message>
sayHi这个消息有一个元素通过ns1:sayHi,找到对应的类型到<wsdl:types>...</wsdl:types>这个标签下去寻找
<xs:complexType name="sayHi">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
通过这段我们可以创建一个xml文档片段
传入消息
<sayHi>
<arg0>字符串</arg0>
</sayHi>
消息传出的xml文档片段
<sayHiResponse>
<return >字符串</return>
</sayHiResponse>
<xs:complexType name="getCatsByUser">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="tns:user"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="user">
<xs:sequence>
<xs:element minOccurs="0" name="address" type="xs:string"/>
<xs:element minOccurs="0" name="id" type="xs:int"/>
<xs:element minOccurs="0" name="name" type="xs:string"/>
<xs:element minOccurs="0" name="pass" type="xs:string"/>
</xs:sequence>
</xs:complexType>
对于另外一个方法,通过阅读wsdl文档,也可以写出对应的xml文档片段
//消息传入
<getCatsByUser>
<arg0>
<address>字符串</address>
<id>整数</id>
<name>字符串</name>
<pass>字符串</pass>
</arg0>
</getCatsByUser>
消息传出
<xs:complexType name="getCatsByUserResponse">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="return" type="tns:cat"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="cat">
<xs:sequence>
<xs:element minOccurs="0" name="color" type="xs:string"/>
<xs:element minOccurs="0" name="id" type="xs:int"/>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<getCatsByUserResponse>
<return >
<color>字符串</color>
<id>整数</id>
<name>字符串</name>
</return>
<return >
<color>字符串</color>
<id>整数</id>
<name>字符串</name>
</return>
</getCatsByUserResponse>
下面是简单的wsdl文档结构图:
第一步:创建一个接口,该接口有二个函数,一个简单一些,另一个复杂一些。
package yy;
import java.util.List;
import javax.jws.WebService;
import yy.domain.Cat;
import yy.domain.User;
@WebService
public interface HelloWorld {
public String sayHi(String name);
public List<Cat> getCatsByUser(User user);
}
第二步:创建一个实现类,去实现该接口:
package yy.impl;
import java.util.Date;
import java.util.List;
import javax.jws.WebService;
import service.UserService;
import service.impl.UserServiceImpl;
import yy.HelloWorld;
import yy.domain.Cat;
import yy.domain.User;
@WebService(endpointInterface="yy.HelloWorld",serviceName="HelloWorldWS")
public class HelloWorldWS implements HelloWorld{
@Override
public String sayHi(String name) {
return name +" 你好"+"现在的时间是"+new Date();
}
@Override
public List<Cat> getCatsByUser(User user) {
//zai shijixiangmuzhong ,Web Service 自己并不会去实现业务逻辑,
//而去调用已有的业务逻辑
UserService service=new UserServiceImpl();
return service.getCatsByUser(user);
}
}
package yy.domain;
public class Cat {
private Integer id;
private String name;
private String color;
public Cat() {
}
public Cat(Integer id, String name, String color) {
super();
this.id = id;
this.name = name;
this.color = color;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
package yy.domain;
public class User {
private Integer id;
private String name;
private String pass;
private String address;
public User() {
}
public User(Integer id, String name, String pass, String address) {
super();
this.id = id;
this.name = name;
this.pass = pass;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((pass == null) ? 0 : pass.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (pass == null) {
if (other.pass != null)
return false;
} else if (!pass.equals(other.pass))
return false;
return true;
}
}
package service;
import java.util.List;
import yy.domain.*;
public interface UserService {
public List<Cat> getCatsByUser(User user);
}
package service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import service.UserService;
import yy.domain.Cat;
import yy.domain.User;
public class UserServiceImpl implements UserService{
static Map<User,List<Cat>> catDb=new HashMap<User,List<Cat>>();
static{
List<Cat> catList1=new ArrayList<Cat>();
catList1.add(new Cat(1,"garefkds","blcak"));
catList1.add(new Cat(2,"fds","bldascak"));
catDb.put(new User(1,"sun","2233","dad"), catList1);
List<Cat> catList2=new ArrayList<Cat>();
catList2.add(new Cat(3,"garefkds","blcak"));
catList2.add(new Cat(4,"fds","bldascak"));
catDb.put(new User(2,"sun","2233","dad"), catList2);
}
@Override
public List<Cat> getCatsByUser(User user) {
return catDb.get(user);
}
}
第三部:创建一个发布类
package lee;
import javax.xml.ws.Endpoint;
public class ServerMain {
public static void main(String[] args){
HelloWorld hello=new HelloWorldWS();
//发布WebService
Endpoint.publish("http://IP/webservie" ,hello);
}
}
然后访问http://IP/webservice?wsdl 就可以看到以下的WSDL文档了。
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.yy/" x
mlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://yy/" name="HelloWorldWS" targetNamespace="http://impl.yy/">
<wsdl:import location="http://202.118.212.80/webservie?wsdl=HelloWorld.wsdl" namespace="http://yy/"></wsdl:import>
<wsdl:binding name="HelloWorldWSSoapBinding" type="ns1:HelloWorld">...</wsdl:binding>
<wsdl:service name="HelloWorldWS">
<wsdl:port binding="tns:HelloWorldWSSoapBinding" name="HelloWorldWSPort">...</wsdl:port>
</wsdl:service>
</wsdl:definitions>
该文档主要包括二部分:
第一部是就是<wsdl:import />代表Web Service的接口的信息
第二部分就是<wsdl:binding></wsdl:binding><wsdl:service></wsdl:service>
详细介绍第一部分:
<wsdl:import location="http://202.118.212.80/webservie?wsdl=HelloWorld.wsdl" namespace="http://yy/"></wsdl:import>
其中location代表的就是发出这个wsdl文档的位置 namespace代表在/yy这个命名空间下。
接下来我们详细的介绍一下 这个接口类中有什么信息 就是http://202.118.212.80/webservie?wsdl=HelloWorld.wsdl
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://yy/" name="HelloWorld" targetNamespace="http://yy/">
<wsdl:types>...</wsdl:types>
<wsdl:message name="getCatsByUserResponse">...</wsdl:message>
<wsdl:message name="sayHiResponse">...</wsdl:message>
<wsdl:message name="getCatsByUser">...</wsdl:message>
<wsdl:message name="sayHi">...</wsdl:message>
<wsdl:portType name="HelloWorld">...</wsdl:portType>
</wsdl:definitions>
WebService的接口类中包含主要的三个标签
----<wsdl:portType name="HelloWorld">...</wsdl:portType>
这个portType代表是哪一个类,它的子元素代表该类有几个方法,每一个方法的输入消息,以及输出消息
<wsdl:portType>
<wsdl:operation name="getCatsByUser">
<wsdl:input message="ns1:getCatsByUser" name="getCatsByUser"></wsdl:input>
<wsdl:output message="ns1:getCatsByUserResponse" name="getCatsByUserResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="sayHi">
<wsdl:input message="ns1:sayHi" name="sayHi"></wsdl:input>
<wsdl:output message="ns1:sayHiResponse" name="sayHiResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
从上面这段我们可以看出,这个接口的名字叫HelloWorld 该类有二个方法,第一个方法叫sayHi 第二个方法叫getCatsByUser
那么我们就会想知道,每一个方法的参数是什么,返回值是什么?接下来我们要看这个操作对应的输入消息是什么
我们拿sayHi这个操作来说:<wsdl:input message="ns1:sayHi" name="sayHi"></wsdl:input>
因此我们需要找到消息的名字是sayHi的消息,即<wsdl:message name="sayHi">...</wsdl:message>
我们详细的解析一下这个:
<wsdl:message name="sayHi">
<wsdl:part element="ns1:sayHi" name="parameters"></wsdl:part>
</wsdl:message>
sayHi这个消息有一个元素通过ns1:sayHi,找到对应的类型到<wsdl:types>...</wsdl:types>这个标签下去寻找
<xs:complexType name="sayHi">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
通过这段我们可以创建一个xml文档片段
传入消息
<sayHi>
<arg0>字符串</arg0>
</sayHi>
消息传出的xml文档片段
<sayHiResponse>
<return >字符串</return>
</sayHiResponse>
<xs:complexType name="getCatsByUser">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="tns:user"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="user">
<xs:sequence>
<xs:element minOccurs="0" name="address" type="xs:string"/>
<xs:element minOccurs="0" name="id" type="xs:int"/>
<xs:element minOccurs="0" name="name" type="xs:string"/>
<xs:element minOccurs="0" name="pass" type="xs:string"/>
</xs:sequence>
</xs:complexType>
对于另外一个方法,通过阅读wsdl文档,也可以写出对应的xml文档片段
//消息传入
<getCatsByUser>
<arg0>
<address>字符串</address>
<id>整数</id>
<name>字符串</name>
<pass>字符串</pass>
</arg0>
</getCatsByUser>
消息传出
<xs:complexType name="getCatsByUserResponse">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="return" type="tns:cat"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="cat">
<xs:sequence>
<xs:element minOccurs="0" name="color" type="xs:string"/>
<xs:element minOccurs="0" name="id" type="xs:int"/>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<getCatsByUserResponse>
<return >
<color>字符串</color>
<id>整数</id>
<name>字符串</name>
</return>
<return >
<color>字符串</color>
<id>整数</id>
<name>字符串</name>
</return>
</getCatsByUserResponse>
下面是简单的wsdl文档结构图:
相关文章推荐
- 函数
- Gentoo源码安装图解
- 第24讲 项目1:分段函数求值
- Android中常用的位图操作(View与Bitmap转化、圆角、灰化、提取Alpha、旋转、倒影、剪切……)
- NOIP2015其余几道题
- HttpURLConnection get post 方式请求 (笔记)
- 【算法】输出斐波那契数列任意一位的值得算法
- Play框架中集成Swagger
- 7、类的扩展
- MTK Android G-Sensor auto detect源码分析
- crontab简易入门
- Android快速SDK(8)毛玻璃效果BlurView【傻瓜模式】
- 黑马程序员——基础知识——线程间的通信
- python数据结构---栈和队列
- 初学杂烩——第四章
- 服务器端tomcat,debug调试
- 欢迎使用CSDN-markdown编辑器
- OC-id、构造方法
- Java语言程序设计基础篇第八版个人笔记(z2015年11月10日19:39:39)
- String, StringBuffer, StringBuilder