本Blog所有內容不得隨意轉載,版權屬于作者所有。如需轉載請與作者聯系( fastzch@163.com )。
未經許可的轉載,本人保留一切法律權益。
一直以來,發現有某些人完全不尊重我的勞動成果,隨意轉載,提醒一下那些人小心哪天惹上官司。
網上關于XFire入門的教程不少,要么是講得很簡單,就像Hello World一樣的程序,要么就是通過IDE集成的工具來開發的,這對于不同的人群有諸多不便,關于XFire的一些詳細的信息就不再多講,可以參考官方網站和相關的文檔,這里講一個完整的入門實例。
實例中包括三個情況,我想基本上可以概括所有的需求,或者自己稍加擴展即可。先來看看我們的Interface。
1
package test;
2
3
import java.util.List;
4
5
public interface IHelloService {
6
public String sayHello(String ttt);
7
8
public Course choose(User u);
9
10
public List test(List t);
11
}
這其中包含了簡單對象的傳遞,對象的傳遞,List的傳遞。
2

3

4

5

6

7

8

9

10

11

具體的開發步驟如下:
1、定義Web Service的接口,代碼見上面的接口定義。
2、實現接口和業務邏輯,代碼如下:
1
package test;
2
3
import java.util.ArrayList;
4
import java.util.List;
5
6
public class HelloServiceImpl implements IHelloService {
7
8
public String sayHello(String ttt) {
9
return "Hello, "+ttt;
10
}
11
12
public Course choose(User u){
13
System.out.println(u.getName());
14
Course c=new Course();
15
c.setName("Eee");
16
return c;
17
18
}
19
20
public List test(List t){
21
for (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);
28
return al;
29
30
}
31
}
用到的User和Course兩個類的代碼如下:
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

1
package test;
2
3
public class User {
4
private String name;
5
6
public String getName() {
7
return name;
8
}
9
10
public void setName(String name) {
11
this.name = name;
12
}
13
}
14

2

3

4

5

6

7

8

9

10

11

12

13

14

1
package test;
2
3
public class Course {
4
private String name;
5
6
public String getName() {
7
return name;
8
}
9
10
public void setName(String name) {
11
this.name = name;
12
}
13
14
}

2

3

4

5

6

7

8

9

10

11

12

13

14

3、編寫XFire要求的WebSevice定義描述文件,如下:
1
<?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>
此文件放在src/META-INF/xfire/services.xml,編譯時會自動編譯到classes的相應目錄下面。
2

3

4

5

6

7

8

9

10

11

最近有些朋友因使用Spring2.0以上版本時,會發生如下異常:



1
<?xml version="1.0" encoding="UTF-8"?>
2
<beans>
3
<service xmlns="http://xfire.codehaus.org/config/1.0"
4
xmlns:s="http://www.springframework.org/schema/beans"
5
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6
xsi: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

2

3

4

5

6

7

8

9

10

11

12

13

4、因為我們用到了List等集合類型,所以需要定義Mapping關系,文件名為:src/test/IHelloService.aegis.xml,代碼如下:
1
<?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>
請注意,此文件一定要放到與IHelloService.java相同的目錄下面,否則會出錯。
2

3

4

5

6

7

8

9

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"
3
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
5
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
6
7
<servlet>
8
<servlet-name>XFireServlet</servlet-name>
9
<servlet-class>
10
org.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>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

此時Web Service的服務端就開發完成了。
我們來看看客戶端的代碼吧,也很簡單,如下:
1
package test;
2
3
import java.net.MalformedURLException;
4
import java.util.ArrayList;
5
import java.util.List;
6
7
import org.codehaus.xfire.XFireFactory;
8
import org.codehaus.xfire.client.XFireProxyFactory;
9
import org.codehaus.xfire.service.Service;
10
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
11
12
public class Client {
13
14
public 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/HelloService";
22
try {
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);
36
for (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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

以上代碼均無注釋,因為都非常簡單,呵呵。如有不清楚的地方,請留言!
關于使用XFire開發Web Service客戶端的方法,可以參考我的另一篇文章《使用XFire開發Web Service客戶端完整入門教程》。