继上一篇《Web Service之Axis1.x入门二》后,这是第三篇。上一篇主要讲的定制发布这种发布方式,那么我们这一篇将讲解传递JavaBean对象。
在上一篇的环境下进行开发。
一.服务端
1.服务端建立一个User类
package com.tankiy.bean;
/**
* @author Tankiy
* @since 2011
*/
public class User {
/**
* 用户标识
*/
private int userId;
/**
* 用户名
*/
private String userName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
2.服务端提供服务类UserServer
package com.tankiy.server;
import com.tankiy.bean.User;
/*
* 服务提供类
* @author Tankiy
* @since 2011
*/
public class UserServer {
/**
* 获取用户类信息
* @return 用户信息
*/
public User getUser() {
User user = new User();
user.setUserId(1);
user.setUserName("Tankiy");
return user;
}
}
3.在server-config.wsdd这个文件的<deployment></deployment>标签之间添加如下内容:
<service name="UserServer" provider="java:RPC" use="literal"> <parameter name="allowedMethods" value="*" /> <parameter name="wsdlPortType" value="UserServer" /> <parameter name="wsdlServicePort" value="UserServer" /> <!-- 所提供的类 --> <parameter name="className" value="com.tankiy.server.UserServer" /> <parameter name="wsdlServiceElement" value="UserServer" /> <!-- 传递的JavaBean对象 --> <beanMapping qname="myNSD:User" xmlns:myNSD="urn:UserServer" languageSpecificType="java:com.tankiy.bean.User"/> </service>
4.wsdl效果如下
二.客户端
1.客户端User类
package client.bean;
import java.io.Serializable;
/**
* 客户端JavaBean
* @author Tankiy
* @since 2011
*/
public class User implements Serializable{
/**
* 用户标识
*/
private int userId;
/**
* 用户名
*/
private String userName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
2.客户端访问的测试类
package client;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import client.bean.User;
/**
* @author Tankiy
* @since 2011
*/
public class UserClient {
/**
* @param args
* @throws MalformedURLException
* @throws Exception
*/
public static void main(String[] args) throws ServiceException, RemoteException, MalformedURLException {
//目标地址
String url = "http://localhost:8999/User/services/UserServer"; //只是这里修改下
//提供接口名
String serviceName = "sayHello";
//创建服务对象
Service service = new Service();
//创建调用对象
Call call = (Call) service.createCall();
//设置调用JavaBean的命名空间
QName qn = new QName("urn:UserServer", "User");
call.registerTypeMapping(User.class, qn, new BeanSerializerFactory(User.class, qn),
new BeanDeserializerFactory(User.class, qn));
call.setTargetEndpointAddress(new URL(url));
//调用的服务器端方法
call.setOperationName(new QName("User", "getUser"));
//设置返回的类型
call.setReturnType(qn, User.class);
User user = (User)call.invoke(new Object[]{});
if(user != null) {
System.out.println(user.getUserName());
}
}
}
输出结果为Tankiy.恭喜你,测试成功。