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的服务器与客户端应用交 ...
随机推荐
- Android资源--颜色RGB值以及名称及样图
颜 色 RGB值 英文名 中文名 #FFB6C1 LightPink 浅粉红 #FFC0CB Pink 粉红 #DC143C Crimson 深红/猩红 #FFF0F5 L ...
- UITabBar背景、icon图标颜色、被选中背景设置以及隐藏UITabBar的两种方式
一.对UITabBar背景和icon图标的一些设置 (1)因为直接给UITabBar设置的背景颜色显示的不纯,半透明的感觉,所以,有时候我们可以直接利用纯色的图片作为背景达到想要的效果: (2)给ic ...
- EffectiveC#02--仅在对基类进行强制更新时才使用new修饰符
1.建议避免使用new修饰符来重新定义非虚函数. 非虚方法是静态绑定的,不管哪里的代码也不管在哪里引用, 它总是严格的调用类中所定义的函数.并不会在运行时在 派生类中查找不同的版本. 2.何时使用ne ...
- cookie那些事
本文面向对cookie有基本了解的读者,小白出门左转 设置cookie (HTTP 响应头) Set-Cookie: {name}={value};path={path};domain={doma ...
- WIN7系统JavaEE(java+tomcat7+Eclipse)环境配
在进行 Java Web环境开发之前,首先要做的第一件事就是搭建开发环境,开发环境搭建成功,接下来便是对整个开发环境进行测试,可以通过编写一个简单的JSP 程序发布到Tomcat应用服务器上运行. 1 ...
- HTML5 本地裁剪图片并上传至服务器(转)
很多情况下用户上传的图片都需要经过裁剪,比如头像啊什么的.但以前实现这类需求都很复杂,往往需要先把图片上传到服务器,然后返回给用户,让用户确定裁剪坐标,发送给服务器,服务器裁剪完再返回给用户,来回需要 ...
- PHP学习笔记三十三【自定义错误处理器】
<?php //自定义错误处理器 //$errorno 错误号 //$errmes错误信息 //这两个参数是必须的 function my_error($errorno,$errmes) { e ...
- 在 Xcode中 修改文件中自动创建的Created by和Copyright
在Xcode里创建的时候,会自动生成注释 // Created byxxx on 15/7/10. // Copyright (c) 2015年 xxxx. All rights reserved ...
- (转) 新手入门:C/C++中的结构体
本文转载于 http://pcedu.pconline.com.cn/empolder/gj/c/0503/567930_all.html#content_page_1 所有程序经过本人验证,部分程序 ...
- Android 开发技术流程
1.网络连接通信 HttpClient 类通信(见<第一行代码> 郭霖2014.8月第一版P385) Android Asynchronous Http Client (见 http: ...