歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> CXF+Spring開發WebService在MyEclipse環境下

CXF+Spring開發WebService在MyEclipse環境下

日期:2017/2/28 13:59:18   编辑:Linux教程

本文采用CXF+Spring開發WebService,開發環境為MyEclipse10,Demo結構圖如下:

具體實現過程:

1.首先在MyEclipse10中新建一個webservice工程,命名為CXFSimpDemo,工程建完之後,右鍵屬性添加CXF類庫,本文使用的是現今最新的2.7.0版本。

2.然後在項目中新建接口和類,具體實現代碼如下所示:

接口IGetName實現:

1 package com.snail;
2 import javax.jws.WebService;
3 @WebService
4 public interface IGetName 
5 {
6     public String getName(String name);
7 }

類GetNameImpl實現:

 1 package com.snail;
 2 import javax.jws.WebService;
 3 @WebService(endpointInterface="com.snail.IGetName")
 4 public class GetNameImpl implements IGetName {
 5     @Override
 6     public String getName(String name) {
 7         return name;
 8     }
 9 
10 }

3.在src裡新建Spring配置文件applicationContext.xml,鑒於CXF本身已經集成了Spring框架,因此我們無須再添加Spring類庫,具體實現代碼如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4         xmlns:jaxws="http://cxf.apache.org/jaxws"
 5         xsi:schemaLocation=" http://www.springframework.org/schema/beans
 6  http://www.springframework.org/schema/beans/spring-beans.xsd
 7  http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" >
 8  
 9         <import resource="classpath:META-INF/cxf/cxf.xml" />
10         <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
11         <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
12         <jaxws:endpoint id="getNameService"
13                implementor="com.snail.GetNameImpl"
14                address="/getName" />
15</beans>

其中jaxws的endpoint的id可以任意更換,implementor配置的是剛剛新建的類GetNameImpl的全路徑,注意如果實現接口就會報錯,adderss也可以任意配置,這個地址名將在最終生成的wsdl文件中得以體現。

4.爾後,配置web.xml文件,webservice啟動,最先讀取的就是web.xml文件,通過配置可以快速讀取CXF框架,並尋找Spring配置文件applicationContext.xml,從而可以發布webservice,具體實現代碼如下:

 1 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 4     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 5     <display-name></display-name>
 6     <welcome-file-list>
 7         <welcome-file>index.jsp</welcome-file>
 8     </welcome-file-list>
 9     <context-param>
10         <param-name>contextConfigLocation</param-name>
11         <param-value> classpath:applicationContext.xml</param-value>
12     </context-param>
13     <listener>
14         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
15     </listener>
16     <servlet>
17         <servlet-name>CXFServlet</servlet-name>
18         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
19         <load-on-startup> 1</load-on-startup>
20     </servlet>
21     <servlet-mapping>
22         <servlet-name>CXFServlet</servlet-name>
23         <url-pattern> /ws/*</url-pattern>
24     </servlet-mapping>
25 </web-app>

其中classpath對應著tomcat中C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\CXFSimpDemo\WEB-INF\classes路徑下的內容,url-pattern標簽如果寫成/*,在後面的wsdl的URL中項目名後面就可以直接跟上jaxws的endpoint的adderss名稱,否則如圖/ws/*,就需要在項目名後面加上ws才能正常訪問wsdl。

5.代碼編譯過後,借助tomcat發布webservice服務,並通過WebServices Explorer測試WebService服務,如圖所示:

輸入測試參數Test Succeed,結果如圖所示:

測試成功,大功告成。

Ubuntu 12.10中MyEclipse 10.6+下載+安裝+破解 http://www.linuxidc.com/Linux/2013-06/86102.htm

安裝MyEclipse10.0 Linux+破解 http://www.linuxidc.com/Linux/2013-04/82212.htm

MyEclipse中配置JDK環境變量 http://www.linuxidc.com/Linux/2013-02/80017.htm

Ubuntu下安裝MyEclipse10和MySQL全程圖解 http://www.linuxidc.com/Linux/2013-01/77869.htm

MyEclipse10 開發 SSH2(Struts2.1+Spring+Hibernate) http://www.linuxidc.com/Linux/2012-07/64859.htm

Ubuntu 13.04 配置MyEclipes 10.7環境 http://www.linuxidc.com/Linux/2014-05/101751.htm

MyEclipse 的詳細介紹:請點這裡
MyEclipse 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved