Web Service中的XFire 传输List 自定义对象.
我把这个创建的步骤和代码的贴出来,.
首先新建一个工程,取名就随便点啦..MyWebService,然后复制jar包到lib目录下, 创建包,建立接口..写一个javaBean的类,
以下是一个简单的不能再简单的接口了,
- package pack.java.xfire.demo;
- import java.util.List;
- /**
- * @author Administrator
- * 服务端接口;
- */
- public interface IPerson {
- public abstract List<Person> getPersonList();
- public abstract List<Person> setPersonList(List<Person> personList);
- public abstract Person getPersonObj();
- }
接下来就是实现这个接口:
- package pack.java.xfire.demo;
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * Service 端,实现类;
- * @author Administrator
- *
- */
- public class PersonImpl implements IPerson,Serializable{
- private static final long serialVersionUID = -9034712983379559694L;
- public List<Person> getPersonList(){
- List<Person> list=new ArrayList<Person>();
- Person person=new Person();
- person.setAge(22);
- person.setName("Zhou");
- person.setSex("Boy");
- list.add(person);
- Person person2=new Person();
- person2.setAge(19);
- person2.setName("Peng");
- person2.setSex("Girl");
- list.add(person2);
- return list;
- }
- 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;
- }
- public List<Person> setPersonList(List<Person> personList){
- return personList;
- }
- }
之后,在你的工程的src目录下建立,META-INF/xfire/services.xml文件,其内容如下:
- <?xml version="1.0" encoding = "utf-8" ?>
- <beans xmlns="http://xfire.codehaus.org/config/1.0">
- <service>
- <name>PersonService</name>
- <namespace>http://simple.java.xifire/IPerson</namespace>
- <serviceClass>pack.java.xfire.demo.IPerson</serviceClass>
- <implementationClass>pack.java.xfire.demo.PersonImpl</implementationClass>
- <scope>request</scope>
- </service>
- </beans>
在启动web 服务器, 在浏览器中输入:
http://localhost:8080/XFireServices/services/
之后就能看到
Available Services:
- PersonService [wsdl]<!--Just filling space according to http://support.microsoft.com/default.aspx?scid=kb;en-us;Q294807--><!--Just filling space according to http://support.microsoft.com/default.aspx?scid=kb;en-us;Q294807-->
点击进去之后,自动产生的一个对应的一些xml配置文件..
配置客户端:
1.建立客户端工程, 拷贝jar包到lib目录下, 然后建立包 和 接口,JavaBean等等, 这里说明一点:
客户端建立的包和接口一样要和服务器端的接口和类一摸一样,还有服务器端的javaBean必须实现Serializable,才能够传输.
否则会找不到,对应的文件, 从服务器返回过来的数据会为null;
在写一个测试类,用来测试服务器的返回的数据,
- package pack.java.xfire.demo;
- 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;
- /**
- * Client 测试端;
- * @author Administrator
- *
- */
- public class ClientDemo {
- /**
- * 主方法;
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String url="http://PC2009120421osa:8080/XFireServices/services/PersonService";
- Service serviceModel=new ObjectServiceFactory().create(IPerson.class);
- try {
- IPerson person=(IPerson) new XFireProxyFactory().create(serviceModel, url);
- List<Person> list=person.getPersonList();
- //输出Person list集合;
- for(Person per:list){
- System.out.println(per.getName()+","+per.getAge()+","+per.getSex());
- }
- List<Person> list2=new ArrayList<Person>();
- Person person2=new Person();
- person2.setAge(11);
- person2.setName("Test");
- person2.setSex("Girl");
- list2.add(person2);
- //设置Person list集合;
- List<Person> personList2=person.setPersonList(list2);
- //输出从服务器端返回的集合数据;
- for (Person per : personList2) {
- System.out.println("\n"+per.getName()+","+per.getAge()+","+per.getSex());
- }
- //调用服务器端的单个对象;
- Person person3 =person.getPersonObj();
- System.out.println("\n"+person3.getName()+","+person3.getAge()+","+person3.getSex());
- Map<Integer,String> map=person3.getMap();
- System.out.println("\n服务器端返回的List集合");
- List<String> list3=person3.getList();
- for (String str : list3) {
- System.out.println(str);
- }
- 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);
- }
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
控制台输出的结果如下:代表成功..
-----------------------------------------------------------------------
Zhou,22,Boy
Peng,19,Girl
Test,11,Girl
ZengPing,22,Girl
服务器端返回的List集合
List1
List2
List3
服务器端返回的Map集合
Web Service中的XFire 传输List 自定义对象.的更多相关文章
- Web Service 中返回DataSet结果大小改进
http://www.cnblogs.com/scottckt/archive/2012/11/10/2764496.html Web Service 中返回DataSet结果方法: 1)直接返回Da ...
- 在Web Service中傳送Dictionary
有個需求,想在Web Service中傳遞Dictionary<string, string>參數,例如: 排版顯示純文字 [WebMethod] public Dictionary< ...
- Web Service 中返回DataSet结果的几种方法
Web Service 中返回DataSet结果的几种方法: 1)直接返回DataSet对象 特点:通常组件化的处理机制,不加任何修饰及处理: 优点:代码精减.易于处理,小数据量处理较快: ...
- ASP.NET Web Service中使用Session 及 Session丢失解决方法 续
原文:ASP.NET Web Service中使用Session 及 Session丢失解决方法 续 1.关于Session丢失问题的说明汇总,参考这里 2.在Web Servcie中使用Sessio ...
- 问题:不支持Dictionary;结果:在Web Service中傳送Dictionary
在Web Service中傳送Dictionary 有個需求,想在Web Service中傳遞Dictionary<string, string>參數,例如: 排版顯示純文字 [WebMe ...
- HashSet存储过程中如何排除不同的自定义对象?
HashSet HashSet存储过程中如何排除不同的自定义对象? 先看一个小demo public class Demo1 { public static void main(String[] ar ...
- 转-Web Service中三种发送接受协议SOAP、http get、http post
原文链接:web服务中三种发送接受协议SOAP/HTTP GET/HTTP POST 一.web服务中三种发送接受协议SOAP/HTTP GET/HTTP POST 在web服务中,有三种可供选择的发 ...
- 企业级SOA之路——在Web Service中使用HTTP和JMS
原文:http://www.tibco.com/resources/solutions/soa/enterprise_class_soa_wp.pdf 概述 IT业界在早期有一种误解,认为 ...
- Web Service中的几个重要术语
WSDL:web service definition language 直译:WebService定义语言 1.对应一种该类型的文件.WSDL 2.定义了Web Service的服务器与客户端应用交 ...
随机推荐
- 常调用的Webservice接口 集合
1. 查询手机:http://www.yodao.com/smartresult-xml/search.s?type=mobile&q=手机号码 2. 查询IP:http://www.yoda ...
- 应对Deadline,时间怎么安排?
问题定义 项目过程中,每项任务都是有时间要求的,一般体现为"截止时间".即Deadline. 怎样安排时间,才干在Deadline之前完毕任务呢? 有效实践 错误做法 在Deadl ...
- hadoop多文件输出
现实环境中,经常遇到一个问题就是想使用多个Reduce,可是迫于setup和cleanup在每个Reduce中会调用一次,仅仅能设置一个Reduce,无法是实现负载均衡. 问题,假设要在reduce中 ...
- Git 笔记一 Git简介
git 笔记一 什么是版本控制 所谓版本控制就是记录对文件的修改记录,这样以后就能回退到需要的 版本.比如你对一段代码进行了几次修改,有几次修改不想要了,如果 使用了版本控制,就可以回退到未做这些修改 ...
- Tomcat 常见问题篇
Tomcat 常见问题一.Tomcat常见问题 1.Tomcat web容器出现故障时,我们通过Tomcat自带的logs查看原因,以下错误提示都是源于logs 2.Connection refuse ...
- [Python学习笔记][Python内置函数]
Python 常用内建函数 比较基础的列表 abs(x) 求绝对值 pow(x,y) 返回x的y次方,等同于x**y round(x[,小数位数]) 对x进行四舍五入,若不指定位数,则返回整数 chr ...
- Android学习笔记--广播(Broadcast)
1.Android广播分类 android的广播类型分为两类:标准广播和有序广播. 标准广播:异步广播,广播发出后,所有注册了的广播接收器都会同时接收到该广播.打个比方:做地铁过程中的语音播报,当列车 ...
- Linq:切勿使用 Count() > 0 来判断集合非空
原文(http://www.cnblogs.com/ldp615/archive/2011/12/11/2284154.html) Linq 出现之前,我们通常使用下面的方式来判断集合是否非空,即集合 ...
- Entity Framework中实现查询的几种方法
在介绍几种方法前,献上一张图,希望图的作者不要追究我的盗图之过.本文的内容是我自学时的笔记,自学的内容来自网络.手打的代码,切不可直接复制过去用,会有好多错别字什么的. Entity SQL 类似于S ...
- 段的创建表user_segments
1.段的定义及类型 Oracle中的段(segment)是占用磁盘空间的一个对象,最常见的段类型包括: l 聚簇cluster l 表table l 表分区 tablepartition l ...