Apache CXF实战.docx
《Apache CXF实战.docx》由会员分享,可在线阅读,更多相关《Apache CXF实战.docx(67页珍藏版)》请在冰豆网上搜索。
ApacheCXF实战
ApacheCXF实战
Apache的CXF现在几乎成了Java领域构建WebService的首选类库,并且它也确实简单易用,下面就通过几篇系列文章做一下简单介绍。
当然首先想到的当然还是那个HelloWorld示例。
这个系列文章中用到的例子都是基于Maven构建的工程,下面是我的pom.xml文件内容
1.//maven.apache.org/POM/4.0.0" xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
2. xsi:
schemaLocation="http:
//maven.apache.org/POM/4.0.0 http:
//maven.apache.org/maven-v4_0_0.xsd">
3. 4.0.0
4. com.googlecode.garbagecan.cxfstudy
5. cxfstudy
6. war
7. 1.0-SNAPSHOT
8. cxfstudy Maven Webapp
9. http:
//maven.apache.org
10.
11.
12. 2.2.7
13.
14.
15.
16.
17. org.apache.cxf
18. cxf-rt-frontend-jaxws
19. ${cxf.version}
20.
21.
22. org.apache.cxf
23. cxf-rt-transports-http
24. ${cxf.version}
25.
26.
27. org.apache.cxf
28. cxf-rt-transports-http-jetty
29. ${cxf.version}
30.
31.
32. org.apache.cxf
33. cxf-rt-ws-security
34. ${cxf.version}
35.
36.
37. org.apache.cxf
38. cxf-rt-ws-policy
39. ${cxf.version}
40.
41.
42. org.apache.cxf
43. cxf-bundle-jaxrs
44. ${cxf.version}
45.
46.
47. javax.ws.rs
48. jsr311-api
49. 1.1.1
50.
51.
52. org.slf4j
53. slf4j-api
54. 1.5.8
55.
56.
57. org.slf4j
58. slf4j-jdk14
59. 1.5.8
60.
61.
62. commons-httpclient
63. commons-httpclient
64. 3.0
65.
66.
67. commons-io
68. commons-io
69. 2.3
70.
71.
72. junit
73. junit
74. 4.8.1
75. test
76.
77.
78.
79.
80. cxfstudy
81.
82.
83. src/main/resources
84.
85.
86. src/main/java
87.
88. **
89.
90.
91. **/*.java
92.
93.
94.
95.
96.
97. org.mortbay.jetty
98. maven-jetty-plugin
99.
100. /
101.
102.
103. 9000
104.
105.
106.
107.
108.
109. org.apache.maven.plugins
110. maven-compiler-plugin
111.
112.
113. 1.5
114.
115.
116.
117.
118.
119.
下面来看看HelloWorld的具体例子。
1.创建HelloWorld接口类
1.package com.googlecode.garbagecan.cxfstudy.helloworld;
2.
3.import javax.jws.WebMethod;
4.import javax.jws.WebParam;
5.import javax.jws.WebResult;
6.import javax.jws.WebService;
7.
8.@WebService
9.public interface HelloWorld {
10. @WebMethod
11. @WebResult String sayHi(@WebParam String text);
12.}
2.创建HelloWorld实现类
1.package com.googlecode.garbagecan.cxfstudy.helloworld;
2.
3.public class HelloWorldImpl implements HelloWorld {
4.
5. public String sayHi(String name) {
6. String msg = "Hello " + name + "!
";
7. return msg;
8. }
9.}
3.创建Server端测试类
1.package com.googlecode.garbagecan.cxfstudy.helloworld;
2.
3.import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
4.
5.// http:
//localhost:
9000/HelloWorld?
wsdl
6.public class Server {
7. public static void main(String[] args) throws Exception {
8. JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
9. factory.setServiceClass(HelloWorldImpl.class);
10.
11. factory.setAddress("http:
//localhost:
9000/ws/HelloWorld");
12. factory.create();
13.
14. System.out.println("Server start...");
15. Thread.sleep(60 * 1000);
16. System.out.println("Server exit...");
17. System.exit(0);
18. }
19.}
4.创建Client端测试类
1.package com.googlecode.garbagecan.cxfstudy.helloworld;
2.
3.import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
4.
5.public class Client {
6. public static void main(String[] args) {
7. JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
8. factory.setServiceClass(HelloWorld.class);
9. factory.setAddress("http:
//localhost:
9000/ws/HelloWorld");
10. HelloWorld helloworld = (HelloWorld) factory.create();
11. System.out.println(helloworld.sayHi("kongxx"));
12. System.exit(0);
13. }
14.}
5.测试
首先运行Server类来启动WebService服务,然后访问http:
//localhost:
9000/ws/HelloWorld?
wsdl地址来确定webservice启动正确。
运行Client测试类,会在命令行输出Hellokongxx!
的message。
书接上文,下面看看CXF怎样和spring集成。
1.创建HelloWorld接口类
1.package com.googlecode.garbagecan.cxfstudy.helloworld;
2.
3.import javax.jws.WebMethod;
4.import javax.jws.WebParam;
5.import javax.jws.WebResult;
6.import javax.jws.WebService;
7.
8.@WebService
9.public interface HelloWorld {
10. @WebMethod
11. @WebResult String sayHi(@WebParam String text);
12.}
2.创建HelloWorld实现类
1.package com.googlecode.garbagecan.cxfstudy.helloworld;
2.
3.public class HelloWorldImpl implements HelloWorld {
4.
5. public String sayHi(String name) {
6. String msg = "Hello " + name + "!
";
7. return msg;
8. }
9.}
3.修改web.xml文件
1.
DOCTYPE web-app PUBLIC
2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3. " >
4.
5.
6.
7. cxfstudy
8.
9.
10. cxf
11. org.apache.cxf.transport.servlet.CXFServlet
12. 1
13.
14.
15.
16. cxf
17. /ws/*
18.
19.
20.
21. org.springframework.web.context.ContextLoaderListener
22.
23.
24.
25. contextConfigLocation
26. classpath*:
**/spring.xml
27.
28.
29.
4.创建spring配置文件并放在classpath路径下
1.
xml version="1.0" encoding="UTF-8"?
>
2.//www.springframework.org/schema/beans"
3. xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance" xmlns:
jaxws="http:
//cxf.apache.org/jaxws"
4. xsi:
schemaLocation="http:
//www.springframework.org/schema/beans http:
//www.springframework.org/schema/beans/spring-beans.xsd
5.http:
//cxf.apache.org/jaxws http:
//cxf.apache.org/schemas/jaxws.xsd">
6. META-INF/cxf/cxf.xml" />
7. META-INF/cxf/cxf-extension-soap.xml" />
8. META-INF/cxf/cxf