Apache CXF实战.docx

上传人:b****1 文档编号:2833668 上传时间:2023-05-04 格式:DOCX 页数:67 大小:37.32KB
下载 相关 举报
Apache CXF实战.docx_第1页
第1页 / 共67页
Apache CXF实战.docx_第2页
第2页 / 共67页
Apache CXF实战.docx_第3页
第3页 / 共67页
Apache CXF实战.docx_第4页
第4页 / 共67页
Apache CXF实战.docx_第5页
第5页 / 共67页
Apache CXF实战.docx_第6页
第6页 / 共67页
Apache CXF实战.docx_第7页
第7页 / 共67页
Apache CXF实战.docx_第8页
第8页 / 共67页
Apache CXF实战.docx_第9页
第9页 / 共67页
Apache CXF实战.docx_第10页
第10页 / 共67页
Apache CXF实战.docx_第11页
第11页 / 共67页
Apache CXF实战.docx_第12页
第12页 / 共67页
Apache CXF实战.docx_第13页
第13页 / 共67页
Apache CXF实战.docx_第14页
第14页 / 共67页
Apache CXF实战.docx_第15页
第15页 / 共67页
Apache CXF实战.docx_第16页
第16页 / 共67页
Apache CXF实战.docx_第17页
第17页 / 共67页
Apache CXF实战.docx_第18页
第18页 / 共67页
Apache CXF实战.docx_第19页
第19页 / 共67页
Apache CXF实战.docx_第20页
第20页 / 共67页
亲,该文档总共67页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

Apache CXF实战.docx

《Apache CXF实战.docx》由会员分享,可在线阅读,更多相关《Apache CXF实战.docx(67页珍藏版)》请在冰点文库上搜索。

Apache CXF实战.docx

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.                    1.5  

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

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2