XFire完整入门教程
本Blog所有内容不得随意转载,版权属于作者所有。如需转载请与作者联系
(fastzch@https://www.doczj.com/doc/7b7305943.html,)。
未经许可的转载,本人保留一切法律权益。
一直以来,发现有某些人完全不尊重我的劳动成果,随意转载,提醒一下那些人小心哪天惹上官司。
网上关于XFire入门的教程不少,要么是讲得很简单,就像Hello World一样的程序,要么就是通过IDE集成的工具来开发的,这对于不同的人群有诸多不便,关于XFire的一些详细的信息就不再多讲,可以参考官方网站和相关的文档,这里讲一个完整的入门实例。
实例中包括三个情况,我想基本上可以概括所有的需求,或者自己稍加扩展即可。先来看看我们的Interface。
1package test;
2
3import java.util.List;
4
5public interface IHelloService {
6public String sayHello(String ttt);
7
8public Course choose(User u);
9
10public List test(List t);
11}
这其中包含了简单对象的传递,对象的传递,List的传递。
具体的开发步骤如下:
1、定义Web Service的接口,代码见上面的接口定义。
2、实现接口和业务逻辑,代码如下:
1package test;
2
3import java.util.ArrayList;
4import java.util.List;
5
6public class HelloServiceImpl implements IHelloService {
7
8public String sayHello(String ttt) {
9return "Hello, "+ttt;
10 }
11
12public Course choose(User u){
13 System.out.println(u.getName());
14 Course c=new Course();
15 c.setName("Eee");
16return c;
17
18 }
19
20public List test(List t){
21for (int i = 0; i < t.size(); i++) {
22 System.out.println((String) t.get(i));
23 }
24 List al=new ArrayList();
25 Course c=new Course();
26 c.setName("EeeDDDDDD");
27 al.add(c);
28return al;
29
30 }
31}
用到的User和Course两个类的代码如下:
1package test;
2
3public class User {
4private String name;
5
6public String getName() {
7return name;
8 }
9
10public void setName(String name) { https://www.doczj.com/doc/7b7305943.html, = name;
12 }
13}
14
1package test;
2
3public class Course {
4private String name;
5
6public String getName() {
7return name;
8 }
9
10public void setName(String name) { https://www.doczj.com/doc/7b7305943.html, = name;
12 }
13
14}
3、编写XFire要求的WebSevice定义描述文件,如下:
1
2
3
4
5
6
7
8
9
10
11
此文件放在src/META-INF/xfire/services.xml,编译时会自动编译到classes的相应目录下面。
最近有些朋友因使用Spring2.0以上版本时,会发生如下异常:
ERROR - Error initializing XFireServlet.
org.springframework.beans.factory.BeanDefinitionStoreException: Unrecognize
d xbean element mapping: beans in namespac
e https://www.doczj.com/doc/7b7305943.html,/config/
1.0
当出现如下异常时,请将此文件用如下内容替换:
1
2
3 4 xmlns:s="https://www.doczj.com/doc/7b7305943.html,/schema/beans" 5 xmlns:xsi="https://www.doczj.com/doc/7b7305943.html,/2001/XMLSchema-instance" 6 xsi:schemaLocation="https://www.doczj.com/doc/7b7305943.html,/schema/beans h ttp://https://www.doczj.com/doc/7b7305943.html,/schema/beans/spring-beans-2.5.xsd"> 7 8 9 10
12
13
4、因为我们用到了List等集合类型,所以需要定义Mapping关系,文件名为:
src/test/IHelloService.aegis.xml,代码如下:
1
2
3
4
5
6
7
8
9
请注意,此文件一定要放到与IHelloService.java相同的目录下面,否则会出错。
5、在Web.xml中配置XFire需要用到的Servlet,代码如下:
1
2 3 xmlns:xsi="https://www.doczj.com/doc/7b7305943.html,/2001/XMLSchema-instance" 4 xsi:schemaLocation="https://www.doczj.com/doc/7b7305943.html,/xml/ns/j2ee 5 https://www.doczj.com/doc/7b7305943.html,/xml/ns/j2ee/web-app_2_4.xsd"> 6 7 8 9 10 org.codehaus.xfire.transport.http.XFireConfigurableServlet 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
此时Web Service的服务端就开发完成了。
我们来看看客户端的代码吧,也很简单,如下:
1package test;
2
3import https://www.doczj.com/doc/7b7305943.html,.MalformedURLException;
4import java.util.ArrayList;
5import java.util.List;
6
7import org.codehaus.xfire.XFireFactory;
8import org.codehaus.xfire.client.XFireProxyFactory;
9import org.codehaus.xfire.service.Service;
10import org.codehaus.xfire.service.binding.ObjectServiceFactory;
11
12public class Client {
13
14public static void main(String[] args) {
15
16 Service srvcModel = new ObjectServiceFactory()
17 .create(IHelloService.class);
18 XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
19 .newInstance().getXFire());
20
21 String helloWorldURL = "http://localhost:8080/xfiretest/services/HelloS ervice";
22try {
23 IHelloService srvc = (IHelloService) factory.create(srvcModel,
24 helloWorldURL);
25 System.out.println(srvc.sayHello("Robin"));
26
27 User u=new User();
28 u.setName("RRRRR");
29 Course c=srvc.choose(u);
30 System.out.println(c.getName());
31
32 List al=new ArrayList();
33 al.add("1212");
34 al.add("2222");
35 List t=srvc.test(al);
36for (int i = 0; i < t.size(); i++) {
37 Course co=(Course)t.get(i);
38 System.out.println(co.getName());
39 }
40
41
42 } catch (MalformedURLException e) {
43 e.printStackTrace();
44 }
45
46 }
47
48}
49
以上代码均无注释,因为都非常简单,呵呵。如有不清楚的地方,请留言!
关于使用XFire开发Web Service客户端的方法,可以参考我的另一篇文章《使用XFire开发Web Service客户端完整入门教程》。