`

初学webservices

    博客分类:
  • ws
阅读更多
首先我把我做webservices应用的流程写出来,有个明了的过程。
 
  • 编写wsdl
  • 生成服务端框架
  • 编写功能类
  • 在服务端框架内调用功能类
  • 发布服务,从生成的deploy.wsdd文件中,拷贝service内容到server-config里面
  • 在web.xml中添加axis servlet
  • 发布工程
  • 使用soapUI测试
  • 编写客户端代码
  •  

  我解释一下,因为一般给你的需求,基本上确定你需要的功能和传入的参数、返回的参数,所以直接编写wsdl,简洁明确。以后的改动若添加功能或者修改参数,仅需要对wsdl文件进行修改。
  我使用的是Axis,在ant调用wsdl文件转化成服务框架代码。每次启动时做,就不用自己动手了。
  外面工程想调用你本地的功能,去获得相应的结果。这个功能类作用就是把传入的参数封装一下,去调用你本地已经写好的功能,然后把获得结果重新封装一下,返回客户想要的结果。功能类就是HelloHelp。
  外部调用你这边的代码,最终是通过该类HelloServiceHttpBindingImpl.java去操作,所以在这里去调用你相应的功能类的方法。因为这个类存在所以不会覆盖该类,这时大家会有疑问,如果我改动了wsdl文件,HelloServiceHttpBindingImpl的内容会变,我是不是又要手动去删除该类。其实只要你的wsdl文件中的功能名字不变,然后定义一个的封装参数类去封装你的传入参数,和传出参数,那样*SoapBindingImpl类就不会变了,你添加或修改参数,都不会影响到*SoapBindingImpl类了。 
  生成的内容里面会有deploy.wsdd文件,你要把你的服务复制到server-config文件中,这样才能发布出去。server-config你可以复制一个,然后替换掉service。这个文件基本上就只需要你动service这块内容,如有复杂点自己在慢慢该,如handler处理,在这就就不介绍了。或者你用命令java org.apache.axis.client.AdminClient deploy.wsdd 这样也可以生成server-config,记得要把axis放在容器里跑起来,然后考入到与web.xml同级目录
  你要想工程处理你的webservice,那么你必须要servlet里面加入axis servlet,这样才会处理你的webservice。
  发布工程。
  然后你就是用soapUI测试你服务端这边是否弄好了。
  最后编写客户端代码。
 
  大家可以看到最后一步操作是在客户端工程里面,前面都是在服务端工程里面。这二个不是同一个工程,服务端工程只要把功能暴露出来,其它工程就可以根据你的wsdl文件去调用。
 
  讲完整个应用的流程后,我具体介绍一下,从上面的步骤大家可以明白,我们主要做的就是2步,写wsdl和写功能类。功能类就需要你自己根据你实际项目去做,我在这里介绍一下编写wsdl。

  WSDL文件主要包含一下内容:
  Types - 数据类型定义。 也就是定义的要传入传出的参数
  Message - 通信消息的数据结构的抽象类型化定义。
  PortType - 对于某个访问入口点类型所支持的操作的抽象集合。主要就是里面定义的operation。
  Operation - 对服务中所支持的操作的抽象描述。这里就是你写你要得功能。
  Binding - 特定端口类型的具体协议和数据格式规范的绑定。通过它暴露你的功能
  Service- 相关服务访问点的集合。包含下面的port
  Port - 定义为协议/数据格式绑定与具体Web访问地址组合的单个服务访问点。访问的地址和端口号。

  举个领子可能大家明白些:

Types:
	<element name="helloRequestParameter">
		<complexType>
			<sequence>
				<element name="name" type="xsd:string" maxOccurs="1"
					minOccurs="1" />
				<element ref="ns:hello" />
			</sequence>
		</complexType>
	</element>

	<element name="hello" type="ns:Hello" />
	
	<complexType name="Hello">
		<sequence>
			<element name="name" type="xsd:string" maxOccurs="1"
				minOccurs="1" />
			<element name="str" type="xsd:string" maxOccurs="1"
				minOccurs="1" />
		</sequence>
	</complexType>

	<element name="helloResponseParameter">
		<complexType>
			<sequence>
				<element name="strOut" type="xsd:string" maxOccurs="1"
					minOccurs="1" />
			</sequence>
		</complexType>
	</element>

元素helloRequestParameter,hello,helloResponseParameter,用Axis生成java文件后,就生成helloRequestParameter,hello,helloResponseParameter 3个类。

Message:
	<message name="helloRequest">
		<part name="helloParameter" element="ns:helloRequestParameter" />
	</message>
	<message name="helloResponse">
		<part name="parameters" element="ns:helloResponseParameter" />
	</message>

这里用我的意思来讲,就是要把你传入,传出参数声明,你样你的2个参数才能使用。part里面name参数名,element参数类型。

PortType,Operation
	<portType name="HelloServicePortType">
		<operation name="sayHello">
			<input message="cs:helloRequest" />
			<output message="cs:helloResponse" />
		</operation>
	</portType>

定义了功能,sayHello方法名,input,output输入,返回参数。

Binding:
<binding name="HelloServiceHttpBinding" type="cs:HelloServicePortType">
		<SOAP:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
		<operation name="sayHello">
			<SOAP:operation soapAction="urn:helloService:webService:com#sayHello" />
			<input>
				<SOAP:body use="literal" />
			</input>
			<output>
				<SOAP:body use="literal" />
			</output>
		</operation>
	</binding>

基本上固定的,不用你修改什么。

service,port:
	<service name="HelloServiceInterface">
		<port name="HelloServiceHttpPort" binding="cs:HelloServiceHttpBinding">
			<SOAP:address location="http://localhost:8080/webServiceTest/services/HelloServiceHttpPort" />
		</port>
	</service>

定义了你的服务名HelloServiceInterface,端口名HelloServiceHttpPort 和发布的地址http://localhost:8080/webServiceTest/services/HelloServiceHttpPort

调用axis的WSDL2Java,会生成6个类
HelloServicePortType              你发布的services接口文件
HelloServiceHttpBindingImpl       实现了services接口,服务器需补充的内容
HelloServiceHttpBindingSkeleton   services的服务端框架代码,实现了services,skeleton接口
HelloServiceInterface             获取services接口文件
HelloServiceInterfaceLocator      实现获取services接口文件
HelloServiceHttpBindingStub       services的客户端存根代码,实现services接口
这几个类包含客户端和服务端的,前面3个是服务端的,后面3个加第一个是客户端的。

这是Ant把wsdl文件转换成java文件
	<target name="convert">
		<java classname="org.apache.axis.wsdl.WSDL2Java" classpathref="lib" fork="true" jvm="${JAVA_JVM}">
			<arg value="-W" />
			<arg value="-a" />
			<arg value="--skeletonDeploy" />
			<arg value="true" />
			<arg value="-o" />
			<arg value="./src" />
			<arg value="./wsdl/Hello.wsdl" />
		</java>
	</target>

这里只是简单的转化了一下,还要考虑到其它问题的,以后再更新。

直接在命令行里面转换wsdl
java org.apache.axis.wsdl.WSDL2Java -W -a --ske
letonDeploy true -o code Hello.wsdl

需要axis  commons-discovery-0.2  commons-loggin-1.04 jaxrpc  wsdl4j-1.5.1这几个类写到环境变量里面,才能在命令行里面调用。

遇到的问题:
1. there is no undefined binding problem?
solution:when you reference the tns:HelloServicePortType and tns:HelloServiceHttpBinding,the xmlms:tns should same to your targetNamespace。
2. Invalid element in com.webService.helloService.schema.HelloRequestParameter - arg0
I debug the code, find the operationName is null cause this problem, so i add call.setOperation(_operations[0]), then it work fine. you can refer the code from *BindingStub.java, and copy the code to your project.

webServiceTest这是服务端代码,我没有把run工程也写在ant里面,所以需要你先调用ant,在跑工程。
TestWebService是客户端代码,客户端的代码,可以根据wsdl文件生成,然后调用。本来也想直接创建call调用,可是没成功,就是遇到的第二个问题,暂时没解决。
经过了调试代码终于发现了问题,菜鸟水平的英语大家还看得懂吧,就是operationName为空引起异常,所以我从HelloServiceHttpBindingStub中把call.setOperation(_operations[0])这段代码考了过来发现没问题了。只是很疑惑为什么我从网上看大家写的客户端调用都没这段代码,难道是跟版本有问题嘛,希望大家指出来。
解压密码123456.

简单的ws了解了一下,有些还没具体弄清楚,希望再接再厉,若有错误,大家一定要指出来。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics