使用xfire编写返回值为list的webservice服务功能描述:平常在项目中使用xfire来写webservice返回值大都为字
使用xfire编写返回值为list的webservice服务
功能描述:平常在项目中使用xfire来写webservice返回值大都为字符串,今天无意间查看了xfire的官方文档,发现用xfire来编写返回值为list的web服务也很简单。但步骤稍微繁琐一点。怕遗忘,特此记录一番。
环境描述:myeclipse6.5+xfire1.2
核心代码:
普通的JavaBean:Users.java(该类是一普通的JavaBean,建议实现java.io.Serializable接口)主要用来实现List<Users>这种功能
package com.xfiredemo.entity;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Users implements Serializable
{
?
private int id;
?private String username;
?private String sex;
?private int age;
?private String memo;
?public int getId()
{
?return id;
?}
?public void setId(int id)
{
?this.id = id;
?}
?public String getUsername()
{
?return username;
?}
?public void setUsername(String username)
{
?this.username = username;
?}
?public String getSex()
{
?return sex;
?}
?public void setSex(String sex)
{
?this.sex = sex;
?}
?public int getAge()
{
?return age;
?}
?public void setAge(int age)
{
?this.age = age;
?}
?public String getMemo()
{
?return memo;
?}
?public void setMemo(String memo)
{
?this.memo = memo;
?}
}
webservice接口IUsersService
package com.xfiredemo.service;
import java.util.List;
import com.xfiredemo.entity.Users;
/** *//**
?* webservice接口
?* @author coder
?*
?*/
public interface IUsersService
{
?
public List<Users> getUsers();
?
}
用于描述该webservice接口中返回值类型的描述文件IUsersService.aegis.xml(该文件必须与webservice中的接口位于同一目录,且命名遵循webservice接口名.aegis.xml 如本文件IUsersService.aegis.xml)
<?xml version="1.0" encoding="UTF-8"?>
?<!--?
该文件用来描述IUsersService接口中getUsers()方法返回值的类型
?该文件必须与IUsersService位于同一目录中,且该文件遵循如下命名规则
?webservice接口名.aegis.xml 如本文件IUsersService.aegis.xml
?-->
<mappings>
?<!-- 映射getUsers方法返回值类型 -->
?<mapping>
?<method name="getUsers">
?<return-type componentType="#users"/>
?</method>
?<!--
?name对应<return-type componentType="#users"/>中的componentType?
class为实体bean的类型即list中存放的类型?
componentType为实体bean的类型
?-->
?<component name="users" class="com.xfiredemo.entity.Users" componentType="com.xfiredemo.entity.Users"/>
?</mapping>
</mappings>
webservice接口实现类UsersServiceImpl.java (构造模拟数据)
package com.xfiredemo.service;
import java.util.ArrayList;
import java.util.List;
import com.xfiredemo.entity.Users;
/**
?* webservice实现类
?* @author coder
?*
?*/
public class UsersServiceImpl implements IUsersService {
?
//返回users对象的集合
?public List<Users> getUsers() {
?
/*******************模拟数据库往list中添加三条模拟数据并返回******************/
?List<Users> list=new ArrayList<Users>();
?Users users=new Users();
?users.setId(1);
?users.setUsername("user1");
?users.setSex("男");
?users.setAge(20);
?users.setMemo("暂无");
?list.add(users);
?
users=new Users();
?users.setId(2);
?users.setUsername("user2");
?users.setSex("男");
?users.setAge(20);
?users.setMemo("暂无");
?list.add(users);
?
users=new Users();
?users.setId(3);
?users.setUsername("user3");
?users.setSex("男");
?users.setAge(20);
?users.setMemo("暂无");
?list.add(users);
?
return list;
?}
?
}
客户端测试代码:UsersServiceClient.java
package com.xfiredemo.service.client;
import java.net.MalformedURLException;
import java.util.List;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.xfiredemo.entity.Users;
import com.xfiredemo.service.IUsersService;
/** *//**
?* webservice客户端测试类
?* @author coder
?*
?*/
public class UsersServiceClient
{
?
public static void main(String[] args)
{
?//构建service
?Service service=new ObjectServiceFactory().create(IUsersService.class);
?try
{
?//获得自定义webservice接口
?IUsersService usersService=(IUsersService) new XFireProxyFactory().create(service, "http://localhost:8080/xfiredemo/services/UsersService");
?//调用该webservice中定义的getUsers()方法
?List<Users> list=usersService.getUsers();
?//输出list长度
?System.out.println(list.size());
?//遍历list
?for (Users users : list)
{
?System.out.println("id:"+users.getId()+"\tusername:"+users.getUsername()+"\tsex:"+users.getSex()+"\tage:"+users.getAge()+"\tmemo:"+users.getMemo());
?}
?} catch (MalformedURLException e)
{
?e.printStackTrace();
?}
?
?
}
}