基于Zend_Soap的webservice及其多语言通信(java)(二)

前者用Zend_Soap完成webservice以后,需要与java进行通信,这里我们只要用一个WSDL文件就可以搞定,当然,这个文件我们已经有了,IE上已经直接显示了,可以把它保存为wsdl文件,其中具体定义的内容比较复杂,我们这里只谈应用。

   java端的环境.

     1)安装jdk。

     2) 用axis1进行与php之间的通信,所以要下载axis1.4。

     3)这里用到了ant与axis1对wsdl进行解译成客户端,所以要安装ant.

     以下是ant任务的定义,主要是为了生成客户端程序。

代码
<?xml version="1.0" encoding="UTF-8"?>
<project name="wsclient" default="all" basedir=".">
<property name="axis.home" value="C:\axis-1_4" />
<property name="options.output" location="../src"/>
<path id="axis.classpath">
<fileset dir="${axis.home}/lib">
<include name="**/*.jar" />
</fileset>
</path>



<taskdef resource="axis-tasks.properties" classpathref="axis.classpath" />

<target name="init">
<echo>Warning: please update the associated WSDL file(s) in the folder wsdl before running the target!</echo>
<echo>Warning: Just run the target(s) related with your developing work!</echo>
<echo>
</echo>
</target>

<target name="-WSDL2Axis" depends="init">
<mkdir dir="${options.output}"/>
<axis-wsdl2java serverside="false" output="${options.output}" url="${options.WSDL-URI}" verbose="true"/>
</target>

<target name="all">
<antcall target="GatewayService"/>

</target>

<target name="GatewayService">
<antcall target="-WSDL2Axis">
<param name="options.WSDL-URI" location="gateway.wsdl"/>
</antcall>
</target>

</project>
生成后的程序结构如下图所示。

将axis1的类库放入到classpath中来,于是调用客户端程序与php通信。

package client;


import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

import localhost.ZFDEMO.admin.soap.index.Custom_Soap_GatewayServiceLocator;
import localhost.ZFDEMO.admin.soap.index.MsgResp;
import localhost.ZFDEMO.admin.soap.index.User;
import localhost.ZFDEMO.admin.soap.index.UserPriority;
import localhost.ZFDEMO.admin.soap.index.Userole;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;


public class SoapClient {

	/**
	 * @param args
	 * @throws ServiceException 
	 * @throws RemoteException 
	 */
	public static void main(String[] args) throws RemoteException, ServiceException {
		
		Custom_Soap_GatewayServiceLocator locator =new Custom_Soap_GatewayServiceLocator();
		locator.setEndpointAddress("Custom_Soap_GatewayPort", "http://localhost/ZFDEMO/admin/soap/index");
		MsgResp logonRsp =locator.getCustom_Soap_GatewayPort().logonUser("***", "1111", "cc");
		MsgResp logoutRsp =locator.getCustom_Soap_GatewayPort().logoutUser("***", "1111");
		Userole userole =locator.getCustom_Soap_GatewayPort().getUserRole("***", "12345");
		UserPriority up =locator.getCustom_Soap_GatewayPort().getUserPriority("***", "12345");
		MsgResp rms =locator.getCustom_Soap_GatewayPort().modifyUser(new String[]{"1","***","男"}, "1111");
		
		System.out.println(TypeUtil.typeToString("msresp for logonUser is ", logonRsp));
		System.out.println(TypeUtil.typeToString("msresp for logoutUser is ", logoutRsp));
		System.out.println(TypeUtil.typeToString("resp for userole is ", userole));
		System.out.println(TypeUtil.typeToString("resp for UserPriority is ", up));
		System.out.println(TypeUtil.typeToString("msresp for modifyUser is ", rms));
		
	}

}

得到返回值为

- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
msresp for logonUser is .user.userid: 1
msresp for logonUser is .user.username: ***
msresp for logonUser is .user.sex: 男
msresp for logonUser is .user.__equalsCalc: null
msresp for logonUser is .user.__hashCodeCalc: false
msresp for logonUser is .code: 0000
msresp for logonUser is .msg: login successfully
msresp for logonUser is .__equalsCalc: null
msresp for logonUser is .__hashCodeCalc: false

msresp for logoutUser is .user.userid: 1
msresp for logoutUser is .user.username: ***
msresp for logoutUser is .user.sex: 男
msresp for logoutUser is .user.__equalsCalc: null
msresp for logoutUser is .user.__hashCodeCalc: false
msresp for logoutUser is .code: 0000
msresp for logoutUser is .msg: logout successfully
msresp for logoutUser is .__equalsCalc: null
msresp for logoutUser is .__hashCodeCalc: false

resp for userole is .username: ***
resp for userole is .userid: 1
resp for userole is .roles[0].roleid: 0
resp for userole is .roles[0].rolename: role_0
resp for userole is .roles[0].parentid: -1
resp for userole is .roles[0].desc: xixi
resp for userole is .roles[0].__equalsCalc: null
resp for userole is .roles[0].__hashCodeCalc: false
resp for userole is .roles[1].roleid: 1
resp for userole is .roles[1].rolename: role_1
resp for userole is .roles[1].parentid: -1
resp for userole is .roles[1].desc: xixi
resp for userole is .roles[1].__equalsCalc: null
resp for userole is .roles[1].__hashCodeCalc: false
resp for userole is .roles[2].roleid: 2
resp for userole is .roles[2].rolename: role_2
resp for userole is .roles[2].parentid: -1
resp for userole is .roles[2].desc: xixi
resp for userole is .roles[2].__equalsCalc: null
resp for userole is .roles[2].__hashCodeCalc: false
resp for userole is .roles[3].roleid: 3
resp for userole is .roles[3].rolename: role_3
resp for userole is .roles[3].parentid: -1
resp for userole is .roles[3].desc: xixi
resp for userole is .roles[3].__equalsCalc: null
resp for userole is .roles[3].__hashCodeCalc: false
resp for userole is .roles[4].roleid: 4
resp for userole is .roles[4].rolename: role_4
resp for userole is .roles[4].parentid: -1
resp for userole is .roles[4].desc: xixi
resp for userole is .roles[4].__equalsCalc: null
resp for userole is .roles[4].__hashCodeCalc: false
resp for userole is .roles[5].roleid: 5
resp for userole is .roles[5].rolename: role_5
resp for userole is .roles[5].parentid: -1
resp for userole is .roles[5].desc: xixi
resp for userole is .roles[5].__equalsCalc: null
resp for userole is .roles[5].__hashCodeCalc: false
resp for userole is .roles[6].roleid: 6
resp for userole is .roles[6].rolename: role_6
resp for userole is .roles[6].parentid: -1
resp for userole is .roles[6].desc: xixi
resp for userole is .roles[6].__equalsCalc: null
resp for userole is .roles[6].__hashCodeCalc: false
resp for userole is .roles[7].roleid: 7
resp for userole is .roles[7].rolename: role_7
resp for userole is .roles[7].parentid: -1
resp for userole is .roles[7].desc: xixi
resp for userole is .roles[7].__equalsCalc: null
resp for userole is .roles[7].__hashCodeCalc: false
resp for userole is .roles[8].roleid: 8
resp for userole is .roles[8].rolename: role_8
resp for userole is .roles[8].parentid: -1
resp for userole is .roles[8].desc: xixi
resp for userole is .roles[8].__equalsCalc: null
resp for userole is .roles[8].__hashCodeCalc: false
resp for userole is .roles[9].roleid: 9
resp for userole is .roles[9].rolename: role_9
resp for userole is .roles[9].parentid: -1
resp for userole is .roles[9].desc: xixi
resp for userole is .roles[9].__equalsCalc: null
resp for userole is .roles[9].__hashCodeCalc: false
resp for userole is .__equalsCalc: null
resp for userole is .__hashCodeCalc: false

resp for UserPriority is .username: ***
resp for UserPriority is .userid: 1
resp for UserPriority is .prioritys[0].prid: 0
resp for UserPriority is .prioritys[0].type: null
resp for UserPriority is .prioritys[0].url: null
resp for UserPriority is .prioritys[0].module: null
resp for UserPriority is .prioritys[0].operation: opti_0
resp for UserPriority is .prioritys[0].desc: null
resp for UserPriority is .prioritys[0].__equalsCalc: null
resp for UserPriority is .prioritys[0].__hashCodeCalc: false
resp for UserPriority is .prioritys[1].prid: 1
resp for UserPriority is .prioritys[1].type: null
resp for UserPriority is .prioritys[1].url: null
resp for UserPriority is .prioritys[1].module: null
resp for UserPriority is .prioritys[1].operation: opti_1
resp for UserPriority is .prioritys[1].desc: null
resp for UserPriority is .prioritys[1].__equalsCalc: null
resp for UserPriority is .prioritys[1].__hashCodeCalc: false
resp for UserPriority is .prioritys[2].prid: 2
resp for UserPriority is .prioritys[2].type: null
resp for UserPriority is .prioritys[2].url: null
resp for UserPriority is .prioritys[2].module: null
resp for UserPriority is .prioritys[2].operation: opti_2
resp for UserPriority is .prioritys[2].desc: null
resp for UserPriority is .prioritys[2].__equalsCalc: null
resp for UserPriority is .prioritys[2].__hashCodeCalc: false
resp for UserPriority is .prioritys[3].prid: 3
resp for UserPriority is .prioritys[3].type: null
resp for UserPriority is .prioritys[3].url: null
resp for UserPriority is .prioritys[3].module: null
resp for UserPriority is .prioritys[3].operation: opti_3
resp for UserPriority is .prioritys[3].desc: null
resp for UserPriority is .prioritys[3].__equalsCalc: null
resp for UserPriority is .prioritys[3].__hashCodeCalc: false
resp for UserPriority is .prioritys[4].prid: 4
resp for UserPriority is .prioritys[4].type: null
resp for UserPriority is .prioritys[4].url: null
resp for UserPriority is .prioritys[4].module: null
resp for UserPriority is .prioritys[4].operation: opti_4
resp for UserPriority is .prioritys[4].desc: null
resp for UserPriority is .prioritys[4].__equalsCalc: null
resp for UserPriority is .prioritys[4].__hashCodeCalc: false
resp for UserPriority is .prioritys[5].prid: 5
resp for UserPriority is .prioritys[5].type: null
resp for UserPriority is .prioritys[5].url: null
resp for UserPriority is .prioritys[5].module: null
resp for UserPriority is .prioritys[5].operation: opti_5
resp for UserPriority is .prioritys[5].desc: null
resp for UserPriority is .prioritys[5].__equalsCalc: null
resp for UserPriority is .prioritys[5].__hashCodeCalc: false
resp for UserPriority is .prioritys[6].prid: 6
resp for UserPriority is .prioritys[6].type: null
resp for UserPriority is .prioritys[6].url: null
resp for UserPriority is .prioritys[6].module: null
resp for UserPriority is .prioritys[6].operation: opti_6
resp for UserPriority is .prioritys[6].desc: null
resp for UserPriority is .prioritys[6].__equalsCalc: null
resp for UserPriority is .prioritys[6].__hashCodeCalc: false
resp for UserPriority is .prioritys[7].prid: 7
resp for UserPriority is .prioritys[7].type: null
resp for UserPriority is .prioritys[7].url: null
resp for UserPriority is .prioritys[7].module: null
resp for UserPriority is .prioritys[7].operation: opti_7
resp for UserPriority is .prioritys[7].desc: null
resp for UserPriority is .prioritys[7].__equalsCalc: null
resp for UserPriority is .prioritys[7].__hashCodeCalc: false
resp for UserPriority is .prioritys[8].prid: 8
resp for UserPriority is .prioritys[8].type: null
resp for UserPriority is .prioritys[8].url: null
resp for UserPriority is .prioritys[8].module: null
resp for UserPriority is .prioritys[8].operation: opti_8
resp for UserPriority is .prioritys[8].desc: null
resp for UserPriority is .prioritys[8].__equalsCalc: null
resp for UserPriority is .prioritys[8].__hashCodeCalc: false
resp for UserPriority is .prioritys[9].prid: 9
resp for UserPriority is .prioritys[9].type: null
resp for UserPriority is .prioritys[9].url: null
resp for UserPriority is .prioritys[9].module: null
resp for UserPriority is .prioritys[9].operation: opti_9
resp for UserPriority is .prioritys[9].desc: null
resp for UserPriority is .prioritys[9].__equalsCalc: null
resp for UserPriority is .prioritys[9].__hashCodeCalc: false
resp for UserPriority is .__equalsCalc: null
resp for UserPriority is .__hashCodeCalc: false

msresp for modifyUser is .user.userid: 1
msresp for modifyUser is .user.username: 男
msresp for modifyUser is .user.sex: ***
msresp for modifyUser is .user.__equalsCalc: null
msresp for modifyUser is .user.__hashCodeCalc: false
msresp for modifyUser is .code: 0000
msresp for modifyUser is .msg: modify successfully
msresp for modifyUser is .__equalsCalc: null
msresp for modifyUser is .__hashCodeCalc: false
这里请看客户端程序中的一段程序。

MsgResp rms =locator.getCustom_Soap_GatewayPort().modifyUser(new String[]{"1","***","男"}, "1111");

这里的一个string的数组,原则上应为Object的数组,因为是传入参数,所以比较好理解,这是因为php端定义了一个array类型的数据结构,如果这里的MsgResp是一个这样的数组你觉得这个数据结构好用吗?所以这里就是前篇文章中所说到的array在多语言通信中的不好的地方。

posted @ 2010-09-01 16:55  游戈  阅读(385)  评论(0编辑  收藏  举报