我把这个创建的步骤和代码的贴出来,.
首先新建一个工程,取名就随便点啦..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(" "+per.getName()+","+per.getAge()+","+per.getSex());
- }
- //调用服务器端的单个对象;
- Person person3 =person.getPersonObj();
- System.out.println(" "+person3.getName()+","+person3.getAge()+","+person3.getSex());
- Map<Integer,String> map=person3.getMap();
- System.out.println(" 服务器端返回的List集合");
- List<String> list3=person3.getList();
- for (String str : list3) {
- System.out.println(str);
- }
- System.out.println(" 服务器端返回的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集合