Eclipse安裝CXF外掛程式開發java web service 整合Spring

來源:互聯網
上載者:User

本文主要介紹在Eclipse[3.3.2]安裝CXF外掛程式。開發一個簡單的java web service,以及和Spring的整合。
安裝外掛程式:
1,下載STP all_in_one,從http://ftp.daum.net/eclipse/stp/old-downloads-page/可以下載stp-all-in-one-incubation-0.7.0.200711162004.zip
安裝這個外掛程式,可以用link檔案的方式安裝,可參考 http://blog.csdn.net/kkdelta/archive/2009/03/12/3983999.aspx
2,下載CXF 運行環境所需的Jar包,

http://people.apache.org/repo/m2-snapshot-repository/org/apache/cxf/apache-cxf/

我用的是 apache-cxf-2.1-incubator-20080414.232258-49.zip
3,開啟eclipse後,在功能表列,windows-->preference-->soa tools 如,說明外掛程式安裝成功。

4,配置CXF運行環境,如,installed Runtimes---- Add--Appach CXF 2.0--Next 指定解壓縮後的apache-cxf-2.1-incubator-20080414.232258-49.zip的路徑。

開發Web Java Service
1,建立一個web project,在 這個project裡建立下面的interface:
package com.test.cxf;
public interface WSprovider {
    public String testWS(String msgIn);
}
然後在這個建好後的project上點右鍵,JAX-WS Tools ---Enable JAX-WS --java first programing mode, 選擇運行cxf環境-- 選擇建立的interface--finish。
你的interface將被加上Java anotation如下:

package com.test.cxf;import javax.jws.WebService;@WebService(name="WSprovider", targetNamespace="http://cxf.test.com/")public interface WSprovider {    public String testWS(String msgIn);}

2,在outline視圖,選中testws(),右鍵選JAX-WX tools--〉create web method
你的interface將被加上Java anotation如下:

@WebService(name="WSprovider", targetNamespace="http://cxf.test.com/")public interface WSprovider {    @WebMethod(operationName="testWS", exclude=false)    @ResponseWrapper(className="com.test.cxf.TestWSResponse", localName="testWSResponse", targetNamespace="http://cxf.test.com/")    @RequestWrapper(className="com.test.cxf.TestWS", localName="testWS", targetNamespace="http://cxf.test.com/")    public String testWS(String msgIn);}

3,然後在project上點右鍵,JAX-WS Tools ---Generate All
會產生interface的實作類別如下:

@WebService(endpointInterface = "com.test.cxf.WSprovider", serviceName = "WSproviderService")                     public class WSproviderImpl implements WSprovider {    public java.lang.String testWS(java.lang.String arg0) {         ........    }}

到此,簡單的web service的開發就算完成了。
整合Spring
1,在WEB-INF下建立一個bean.xml檔案:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:jaxws="http://cxf.apache.org/jaxws"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">    <import resource="classpath:META-INF/cxf/cxf.xml" />    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />    <bean name="testService" class="com.test.cxf.WSCXFProviderImpl"/>       <jaxws:endpoint        id="testEndpoint"        implementor="#testService"        wsdlLocation="classpath:wsdl/prjCXFWeb.wsdl"        address="/WSCXFProviderPort">        <jaxws:features>            <bean class="org.apache.cxf.feature.LoggingFeature"/>        </jaxws:features>    </jaxws:endpoint>     </beans>

2,對產生的wsdl檔案修改:
把<soap:address location="http://localhost:9090/WSproviderPort"/>
改成<soap:address location="http://localhost:8080/prjCXFWeb/services/WSCXFProviderPort" />
在你的src下建立一個wsdl檔案,把改後的wsdl copy到此【為了對應bean.xml中的wsdlLocation】。

3,在web.xml中加入:

  <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>WEB-INF/beans.xml</param-value>    </context-param>    <listener>        <listener-class>            org.springframework.web.context.ContextLoaderListener        </listener-class>    </listener>    <servlet>        <servlet-name>CXFServlet</servlet-name>        <servlet-class>            org.apache.cxf.transport.servlet.CXFServlet        </servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>CXFServlet</servlet-name>        <url-pattern>/services/*</url-pattern>    </servlet-mapping>

4,將web project發布到web container(e.g tomcat)中,web service便可以被調用了。
在IE中輸入 http://localhost:8080/prjCXFWeb/services/WSCXFProviderPort?wsdl,能看到wsdl檔案,證明
web service發布成功了。

如果不和Spring整合,可以自己實現一個CXFNonSpringServlet的servlet,在web.xml中配置這個servlet來處理web service的請求.

public class SimpleServlet extends CXFNonSpringServlet {    private static final long serialVersionUID = 1L;    public void loadBus(ServletConfig servletConfig) throws ServletException {        super.loadBus(servletConfig);        BusFactory.setDefaultBus(getBus());        Object implementor = new WSCXFProviderImpl();        Endpoint.publish("/p1", implementor);    }}    <!-- not using Spring -->    <servlet>        <servlet-name>CXFServlet</servlet-name>        <servlet-class>            com.bt.cxf.ws.SimpleServlet        </servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>CXFServlet</servlet-name>        <url-pattern>/services/*</url-pattern>    </servlet-mapping>

在IE中輸入 http://localhost:8080/prjCXFWeb/services/p1?wsdl,能看到wsdl檔案,證明

web service發布成功了。[p1對應 ndpoint.publish("/p1", implementor);]

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.