webservice cxf 執行個體

來源:互聯網
上載者:User

標籤:style   class   c   java   tar   ext   

轉自百度空間:http://hi.baidu.com/cpuhandou/item/b8b439860afb99c9ee083d65

CXF webservice 開發入門

1.       建立一個JavaWebProject,命名為cxfDemo

選擇【next】,為project添加userLib庫。
       

2.       開啟建立的project,在src下建立包com.handou.cxf.test。在包中建立inteface名稱為HelloWorld
代碼如下:
@WebService
public interface HelloWorld {
    @WebMethod
    public String sayHello(@WebParam(name="name")String name);

}

建立一個class為該介面的實作類別,其代碼如下:
public class HelloWorldImpl implements HelloWorld {
    public String sayHello(String name) {
       // TODO Auto-generated method stub
       return name.concat(" , Hello WebService!!!");
    }
}

3.       發布webservice。
採用cxf內建的Jetty伺服器發布服務。在HelloWorldImpl的實作類別中添加main()方法,具體如下:
public static void main(String[] args) {
      HelloWorldImpl implementor = new HelloWorldImpl();
      JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
      svrFactory.setServiceClass(HelloWorld.class);
      svrFactory.setAddress("http://localhost:8080/helloWorld");
      svrFactory.setServiceBean(implementor);
      svrFactory.getInInterceptors().add(new LoggingInInterceptor());
      svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
      svrFactory.create();
   }

4.       在地址欄輸入http://localhost:8080/helloWorld?wsdl,查看wsdl詳細資料




5.       訪問webservice服務端。
方法1:由wsdl可知介面只有一個供調用的operation,方法名稱為sayHello,輸入參數變數名稱為name,資料類型為string,採用地址欄url?+參數測試,輸入:
http://localhost:8080/helloWorld/sayHello?name=HanDou,顯示返回資訊

方法2:用戶端編碼調用。
        建立Java類HelloClient,其代碼如下:
       public class HelloClient {
    /**
     * @param args
     */
    public static void main(String[] args) {   
       JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
       //factory.getInInterceptors().add(new LoggingInInterceptor());
       factory.getOutInterceptors().add(new LoggingOutInterceptor());
       factory.setServiceClass(HelloWorld.class);
       factory.setAddress("http://localhost:8080/helloWorld");
       HelloWorld client = (HelloWorld) factory.create();
       String reply = client.sayHello("HanDou");
       System.out.println("Server said: " + reply);
       System.exit(0);
    }
}

控制台顯示反饋訊息:



6.       採用spring+cxf整合發布webservice。

1.在WEB-INF下建立beans.xml,beans.xml中添加cxf配置。
    <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.xsd
       http://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" />

    <jaxws:endpoint 
      id="helloWorld" 
      implementor="com.handou.cxf.test.HelloWorldImpl" 
      address="/HelloWorld"/>
</beans>

2.在web.xml中添加spring和cxf的配置
       <web-app>
    <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>
       <display-name>CXF Servlet</display-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>/*</url-pattern>
    </servlet-mapping>
</web-app>

3.部署檔案至伺服器,啟動伺服器,通過IP地址+連接埠+工程名+webservice服務名。 
先訪問工程,

 

點擊wsdl進入http://localhost:8080/cxf/HelloWorld?wsdl,可獲得wsdl詳細資料

#webservice/xml/esb

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.