您的位置:首页 > 其它

soap基于契约优先WSDL的开发

2014-04-27 16:09 232 查看
实现一个增加、删除的功能

步骤一:在项目类路径下面新建META-INF目录,在此目录下新建wsdl4andy.wsdl文件,文件如下



<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
		xmlns:wns="http://www.wuchuanlong.cn/mywsdl/" 
		xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
		xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
		name="MyServiceImplService" 
		targetNamespace="http://www.wuchuanlong.cn/mywsdl/">
 <!-- 
 	开发步骤:
 		1、编写schema或者wsdl文件
 		2、根据这个文件生成一个客户端代码
 		3、编写实现类,在实现类上指定wsdlLocation
 		4、发布服务
  	wsdl的结构:
  		types:定义数据类型(方法等)。
  				webservice使用的数据类型,他是独立于机器和语言的类型定义这些数据类型定义被message标签使用
  		message:为每个方法和返回值编写消息。
  				webservice使用的消息,它定义了webservice函数的参数,在wsdl中,输入参数和输出参数要分开定义,
  				使用不同的message标签体标识,所定义的输入输出参数被portType标签使用
  		portType:指定接口和方法,以及该方法的输入输出参数。
  				 webservice执行的操作,引用message标签的定义来描述函数签名(操作数、输入输出参数等)
  		binding:指定编码样式以及接口的实现
  				webservice使用的通讯协议,portType标签中定义的每一个在此绑定实现
  		service:确定每一个binding标签的端口地址
  		
  -->
  
  <!-- 定义数据类型(方法等) -->
  <wsdl:types>
  		<xsd:schema targetNamespace="http://www.wuchuanlong.cn/mywsdl/">
  			<!-- 定义两个方法和返回值 -->
  			<xsd:element name="plus" type="wns:plus"></xsd:element>
  			<xsd:element name="divide" type="wns:divide"></xsd:element>
  			<xsd:element name="plusResponse" type="wns:plusResponse"></xsd:element>
  			<xsd:element name="divideResponse" type="wns:divideResponse"></xsd:element>
  			<!-- 定义元素的类型 -->
  			<xsd:complexType name="plus">
  				<xsd:sequence>
  					<xsd:element name="paramA" type="xsd:int"></xsd:element>
  					<xsd:element name="paramB" type="xsd:int"></xsd:element>
  				</xsd:sequence>
  			</xsd:complexType>
  			<xsd:complexType name="plusResponse">
  				<xsd:sequence>
  					<xsd:element name="plusResult" type="xsd:int"></xsd:element>
  				</xsd:sequence>
  			</xsd:complexType>
  			<xsd:complexType name="divide">
  				<xsd:sequence>
  					<xsd:element name="paramA" type="xsd:int"></xsd:element>
  					<xsd:element name="paramB" type="xsd:int"></xsd:element>
  				</xsd:sequence>
  			</xsd:complexType>
  			<xsd:complexType name="divideResponse">
  				<xsd:sequence>
  					<xsd:element name="divideResult" type="xsd:int"></xsd:element>
  				</xsd:sequence>
  			</xsd:complexType>
  		</xsd:schema>
  </wsdl:types>
  
  <!-- 为每个方法和返回值编写消息 -->
  <wsdl:message name="plus">
  		<wsdl:part name="plus" element="wns:plus"></wsdl:part>
  </wsdl:message>
  <wsdl:message name="plusResponse">
  		<wsdl:part name="plusResponse" element="wns:plusResponse"></wsdl:part>
  </wsdl:message>
  <wsdl:message name="divide">
  		<wsdl:part name="divide" element="wns:divide"></wsdl:part>
  </wsdl:message>
  <wsdl:message name="divideResponse">
  		<wsdl:part name="divideResponse" element="wns:divideResponse"></wsdl:part>
  </wsdl:message>
  
  <!-- 指定接口和方法,以及该方法的输入输出参数 -->
  <wsdl:portType name="MyService">
  		<wsdl:operation name="plus">
  			<wsdl:input message="wns:plus"></wsdl:input>
  			<wsdl:output message="wns:plusResponse"></wsdl:output>
  		</wsdl:operation>
  		<wsdl:operation name="divide">
  			<wsdl:input message="wns:divide"></wsdl:input>
  			<wsdl:output message="wns:divideResponse"></wsdl:output>
  		</wsdl:operation>
  </wsdl:portType>
  
  
  <!--指定编码样式以及接口的实现  -->
  <wsdl:binding name="myServiceSoap" type="wns:MyService">
  		<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
  		<wsdl:operation name="plus">
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
  		</wsdl:operation> 
  		<wsdl:operation name="divide">
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
  		</wsdl:operation> 
  </wsdl:binding>
  
  <wsdl:service name="MyServiceImplService">
    <wsdl:port binding="wns:myServiceSoap" name="MyServiceImplPort">
      <soap:address location="http://127.0.0.1:8989/"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>
步骤二:打开cmd,进入到wsdl4andy.wsdl目录下,输入命令 wsimport -d e:\wsdl -keep wsdl4andy.wsdl

步骤三:将导出的文件放置项目src目录下,编写接口实现类如下:

@WebService(endpointInterface="cn.wuchuanlong.mywsdl.MyService",
			wsdlLocation="META-INF/mywsdl.wsdl",
			targetNamespace = "http://www.wuchuanlong.cn/mywsdl/")
public class MyServiceImpl implements MyService{

	@Override
	public int plus(int paramA, int paramB) {
		System.out.println(paramA+paramB);
		return paramA+paramB;
	}

	@Override
	public int divide(int paramA, int paramB) {
		System.out.println(paramA/paramB);
		return paramA/paramB;
	}

}

步骤四:启动服务public class Test {
	public static void main(String[] args) {
		Endpoint.publish("http://127.0.0.1:8989/contract", new MyServiceImpl());
	}
}
步骤五:打开cmd,输入命令 wsimport -d e:\wsdl2 -keep http://127.0.0.1:8989/contract?wsdl
步骤六:把e:\wsdl2下的文件拷贝至项目下面

步骤七:编写测试类
public class Test {
	public static void main(String[] args) {
		MyServiceImplService msis = new MyServiceImplService();
		MyService ms = msis.getMyServiceImplPort();
		System.out.println(ms.divide(5, 5));;
	}
}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: