Web Service中的XFire 传输List、Map 自定义对象

简单记录XFire中怎么传输对象的实现,以备查阅;又不正确的地方请看官们指正!

 

1、先来看看我们的Interface

package com.wy;

import java.util.List;

/**
 * 服务端接口;
 * @author Administrator
 *
 */
public interface IPerson {
	public String example(String message);
	public abstract List<Person> getPersonList();  
    public abstract List<Person> setPersonList(List<Person> personList);  
    public abstract Person getPersonObj(); 
}

 

 

这其中包含了简单对象的传递,对象的传递,List的传递。

 

2、实现接口和业务逻辑

    

package com.wy;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PersonImpl implements IPerson, Serializable {

	private static final long serialVersionUID = 1L;
    
	public String example(String message) {
		return "你好,"+message;
	}
	
	public List<Person> getPersonList() {
		List<Person> list = new ArrayList<Person>();
		Person person = new Person();
		person.setAge(22);
		person.setName("Wang");
		person.setSex("Boy");
		list.add(person);
		Person person2 = new Person();
		person2.setAge(19);
		person2.setName("Sun");
		person2.setSex("Girl");
		list.add(person2);
		return list;
	}

	public List<Person> setPersonList(List<Person> personList) {
		return personList;
	}

	public Person getPersonObj() {
		Person person = new Person();
		person.setAge(22);
		person.setName("ZengPing");
		person.setSex("Girl");
		Map<Integer, String> map = new HashMap<Integer, String>();
		map.put(1, "HuFang");
		map.put(2, "DuanYouYu");
		map.put(3, "TianWei");
		List<String> list = new ArrayList<String>();
		list.add("List1");
		list.add("List2");
		list.add("List3");
		person.setList(list);
		person.setMap(map);
		return person;
	}

}

 

  用到的Person,必须实现Serializable,才能够传输.否则会找不到,对应的文件, 从服务器返回过来的数据会为null;

 

 

package com.wy;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * Entry
 * @author Administrator
 *
 */
public class Person implements Serializable{
	
	private static final long serialVersionUID = 1L;
	private String name;
	private int age;
	private String sex;
	private List<String> list;
	private Map<Integer, String> map;

	public Person() {
	}

	public Person(String name, int age, String sex,List<String> list,Map<Integer, String> map) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.list = list;
		this.map = map;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public List<String> getList() {
		// TODO Auto-generated method stub
		return list;
	}

	public void setList(List<String> list) {
		// TODO Auto-generated method stub
		this.list = list;
	}

	public void setMap(Map<Integer, String> map) {
		// TODO Auto-generated method stub
		this.map = map;
	}

	public Map<Integer, String> getMap() {
		// TODO Auto-generated method stub
		return map;
	}

}

 

 

3、在src/META-INF/xfire/services.xml,编译时会自动编译到classes的相应目录下面

    

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
	
	<service>
		<name>PersonService</name>
		<serviceClass>com.wy.IPerson</serviceClass>
		<implementationClass>
			com.wy.PersonImpl
		</implementationClass>
		<scope>request</scope>
	</service>
</beans>

 

 

4、因为用到了List、Map等集合类型,所以需要定义Mapping关系,文件名为:src/test/IPerson.aegis.xml

    

<?xml version="1.0" encoding="UTF-8"?>
<mappings>
	<mapping>
		<method name="getList">
			<parameter index="0" componentType="java.lang.String" />
			<return-type componentType="com.wy.Person" />
		</method>
	</mapping>
	
	<mapping>
		<method name="getMap">
			<parameter index="0" componentType="java.lang.String" />
			<return-type componentType="com.wy.Person" />
		</method>
	</mapping>
</mappings>

 

   注意,此文件一定要放到与IHelloService.java相同的目录下面,否则会出错。

 

  5、在Web.xml中配置XFire需要用到的Servlet

     

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<servlet>
		<servlet-name>XFireServlet</servlet-name>
		<servlet-class>
			org.codehaus.xfire.transport.http.XFireConfigurableServlet
		</servlet-class>
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>XFireServlet</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

此时Web Service的服务端就开发完成了。

 

现在来看看客户端的代码吧

 

package com.wy;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

public class Client {
	/**
	 * 主方法;
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String url = "http://localhost:8080/WebServiceXFire/services/PersonService";
		Service serviceModel = new ObjectServiceFactory().create(IPerson.class);
		try {
			IPerson person = (IPerson) new XFireProxyFactory().create(
					serviceModel, url);
			//
			System.out.println(person.example("WebService!"));
			System.out.println("-----------------1------------------");
			List<Person> list = person.getPersonList();
			// 输出Person list集合;
			for (Person per : list) {
				System.out.println(per.getName() + "," + per.getAge() + ","
						+ per.getSex());
			}
			System.out.println("-----------------2------------------");
			List<Person> list2 = new ArrayList<Person>();
			Person person2 = new Person();
			person2.setAge(20);
			person2.setName("Test");
			person2.setSex("Girl");
			list2.add(person2);
			
			// 设置Person list集合;
			List<Person> personList2 = person.setPersonList(list2);
			// 输出从服务器端返回的集合数据;
			for (Person per : personList2) {
				System.out.println(per.getName() + "," + per.getAge()
						+ "," + per.getSex());
			}
			System.out.println("-----------------3------------------");
			// 调用服务器端的单个对象;
			Person person3 = person.getPersonObj();
			System.out.println(person3.getName() + ","
					+ person3.getAge() + "," + person3.getSex());
			System.out.println("-----------------4------------------");
			System.out.println("\n服务器端返回的List集合");
			List<String> list3 = person3.getList();
			for (String str : list3) {
				System.out.println(str);
			}
			System.out.println("------------------5-----------------");
			Map<Integer, String> map = person3.getMap();
			System.out.println("\n服务器端返回的Map集合");
			for (Iterator<Integer> iterator = map.keySet().iterator(); iterator
					.hasNext();) {
				int resultKey = iterator.next();
				String resultValue = map.get(resultKey);
				System.out.println(resultKey + "," + resultValue);
			}
			System.out.println("-------------------6----------------");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

 

  

 

你可能感兴趣的:(java,xml,Web,servlet,javaee)