引用
力宝 的 xfire教程
网上关于XFire入门的教程不少,要么是讲得很简单,就像Hello World一样的程序,要么就是通过IDE集成的工具来开发的,这对于不同的人群有诸多不便,关于XFire的一些详细的信息就不再多讲,可以参考官方网站和相关的文档,这里讲一个完整的入门实例。
实例中包括三个情况,我想基本上可以概括所有的需求,或者自己稍加扩展即可。先来看看我们的Interface。1这其中包含了简单对象的传递,对象的传递,List的传递。package test;
2![]()
3imp
ort 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}
具体的开发步骤如下:
1、定义Web Service的接口,代码见上面的接口定义。
2、实现接口和业务逻辑,代码如下:
1用到的User和Course两个类的代码如下:package test;
2
3imp
ort java.util.ArrayList;
4imp
ort 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){
13System.out.println(u.getName());
14Course c=new Course();
15c.setName("Eee");
16return c;
17![]()
18}
19![]()
20public List test(List t){
21for (int i = 0; i < t.size(); i++) {
22System.out.println((String) t.get(i));
23}
24List al=new ArrayList();
25Course c=new Course();
26c.setName("EeeDDDDDD");
27al.add(c);
28return al;
29![]()
30}
31}
1package test;
2
3public class User {
4private String name;
5
6public String getName() {
7return name;
8}
9
10public void setName(String name) {
11this.name = name;
12}
13}
141package test;
2
3public class Course {
4private String name;
5
6public String getName() {
7return name;
8}
9
10public void setName(String name) {
11this.name = name;
12}
13
14}
3、编写XFire要求的WebSevice定义描述文件,如下:
1此文件放在src/META-INF/xfire/services.xml,编译时会自动编译到classes的相应目录下面。<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://xfire.codehaus.org/config/1.0">
3
4<service>
5<name>HelloService</name>
6<namespace>http://test/HelloService</namespace>
7<serviceClass>test.IHelloService</serviceClass>
8<implementationClass>test.HelloServiceImpl</implementationClass>
9</service>
10![]()
11</beans>
最近有些朋友因使用Spring2.0以上版本时,会发生如下异常:
当出现如下异常时,请将此文件用如下内容替换:ERROR - Error initializing XFireServlet.
org.springframework.beans.factory.BeanDefinitionStoreException: Unrecognized xbean element mapping: beans in namespace http://xfire.codehaus.org/config/1.0
1<?xml version="1.0" encoding="UTF-8"?>
2<beans>
3<service xmlns="http://xfire.codehaus.org/config/1.0"
4xmlns:s="http://www.springframework.org/schema/beans"
5xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
7<name>HelloService</name>
8<namespace>http://test/HelloService</namespace>
9<serviceClass>test.IHelloService</serviceClass>
10<implementationClass>test.HelloServiceImpl</implementationClass>
11</service>
12</beans>
13
4、因为我们用到了List等集合类型,所以需要定义Mapping关系,文件名为:src/test/IHelloService.aegis.xml,代码如下:
1请注意,此文件一定要放到与IHelloService.java相同的目录下面,否则会出错。<?xml version="1.0" encoding="UTF-8"?>
2<mappings>
3<mapping>
4<method name="test">
5<parameter index="0" componentType="java.lang.String" />
6<return-type componentType="test.Course" />
7</method>
8</mapping>
9</mappings>
5、在Web.xml中配置XFire需要用到的Servlet,代码如下:
1<?xml version="1.0" encoding="UTF-8"?>
2<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
5http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
6
7<servlet>
8<servlet-name>XFireServlet</servlet-name>
9<servlet-class>
10org.codehaus.xfire.transport.http.XFireConfigurableServlet
11</servlet-class>
12</servlet>
13
14<servlet-mapping>
15<servlet-name>XFireServlet</servlet-name>
16<url-pattern>/servlet/XFireServlet/*</url-pattern>
17</servlet-mapping>
18
19<servlet-mapping>
20<servlet-name>XFireServlet</servlet-name>
21<url-pattern>/services/*</url-pattern>
22</servlet-mapping>
23
24
25<welcome-file-list>
26<welcome-file>index.jsp</welcome-file>
27</welcome-file-list>
28</web-app>
此时Web Service的服务端就开发完成了。
我们来看看客户端的代码吧,也很简单,如下:
1package test;
2
3imp
ort java.net.MalformedURLException;
4imp
ort java.util.ArrayList;
5imp
ort java.util.List;
6
7imp
ort org.codehaus.xfire.XFireFactory;
8imp
ort org.codehaus.xfire.client.XFireProxyFactory;
9imp
ort org.codehaus.xfire.service.Service;
10imp
ort org.codehaus.xfire.service.binding.ObjectServiceFactory;
11
12public class Client {
13
14public static void main(String[] args) {
15
16Service srvcModel = new ObjectServiceFactory()
17.create(IHelloService.class);
18XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
19.newInstance().getXFire());
20
21String helloWorldURL = "http://localhost:8080/xfiretest/services/HelloService";
22try {
23IHelloService srvc = (IHelloService) factory.create(srvcModel,
24helloWorldURL);
25System.out.println(srvc.sayHello("Robin"));
26![]()
27User u=new User();
28u.setName("RRRRR");
29Course c=srvc.choose(u);
30System.out.println(c.getName());
31![]()
32List al=new ArrayList();
33al.add("1212");
34al.add("2222");
35List t=srvc.test(al);
36for (int i = 0; i < t.size(); i++) {
37Course co=(Course)t.get(i);
38System.out.println(co.getName());
39}
40![]()
41![]()
42} catch (MalformedURLException e) {
43e.printStackTrace();
44}
45
46}
47
48}
49