XFire入門實例

<!-- /* Font Definitions */ @font-face {font-family:宋體; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} @font-face {font-family:"/@宋體"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋體; mso-font-kerning:1.0pt;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:1099569772; mso-list-type:hybrid; mso-list-template-ids:2017498380 -427549864 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-start-at:2; mso-level-number-format:japanese-counting; mso-level-text:%1、; mso-level-tab-stop:54.0pt; mso-level-number-position:left; margin-left:54.0pt; text-indent:-36.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->

理論知識看相關資料,在此就不復述,直接進入XFire 開發實踐。

一、下載 Xfire ,目前已更新爲 CXF ,例子程序用的是 XFire1.2.6

二、   添加 xfire-all-1.2.6.jar 及其依賴包到類路徑,依賴包在 lib 文件夾中

三、   開發 webservice 服務端 HelloWorldService

1 、服務接口:

package hellows;

//Generated by MyEclipse

 

public interface IHelloWorld {

   

          public String example(String message);

   

}

2 、服務實現類

package hellows;

//Generated by MyEclipse

 

public class HelloWorldImpl implements IHelloWorld {

   

    public String example(String message) {

       System. out .println( this );

       return " 你好 , 這是我的第一個 Web Service, 你輸入的消息是 :" +

       message;

    }

}

3 Service.xml

<? xml version = "1.0" encoding = "UTF-8" ?>

< beans xmlns = "http://xfire.codehaus.org/config/1.0" >

 

    < service >

       < name > HelloWorld </ name >

       < serviceClass > hellows.IHelloWorld </ serviceClass >

       < implementationClass > hellows.HelloWorldImpl </ implementationClass >

       < style > wrapped </ style >

       < use > literal </ use >

       < scope > application </ scope >

    </ service ></ beans >

4 Web.xml

<? xml version = "1.0" encoding = "UTF-8" ?>

< web-app xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" version = "2.5" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >

  < servlet >

    < servlet-name > XFireServlet </ servlet-name >

    < servlet-class > org.codehaus.xfire.transport.http.XFireConfigurableServlet </ servlet-class >

    < load-on-startup > 0 </ load-on-startup >

  </ servlet >

  < servlet-mapping >

    < servlet-name > XFireServlet </ servlet-name >

    < url-pattern > /services/* </ url-pattern >

  </ servlet-mapping >

  < welcome-file-list >

    < welcome-file > index.jsp </ welcome-file >

  </ welcome-file-list >

</ web-app >

    部署到web 容器,如tomcat, 啓動web 容器,在瀏覽器中輸入 http://localhost:8080/HelloWorldService/services/HelloWorld wsd l 即可看到相應的wsdl 文件,其中8080web 容器訪問接口。

四、開發 webservice 客戶端 HelloWorldClient ,使用服務

使用 webService 服務有幾種方式,分別實現如下:

1

import java.net.MalformedURLException;

import javax.wsdl.Service;

import org.codehaus.xfire.XFireFactory;

import org.codehaus.xfire.client.XFireProxyFactory;

import org.codehaus.xfire.service.binding.ObjectServiceFactory;

public class POJOClient {

    public static void main(String[] args) {

   

       org.codehaus.xfire.service.Service srvcModel = new ObjectServiceFactory().create(IHelloWorld.class);

       XFireProxyFactory factory = new XFireProxyFactory(

         XFireFactory.newInstance().getXFire());

       String IHelloWorldWorldURL = "http://localhost:8080/HelloWorldService/services/HelloWorld";

       IHelloWorld srvc;

       try {

           srvc = ( IHelloWorld) factory.create(srvcModel,

                                              IHelloWorldWorldURL);

           System.out.println(srvc.example("Robin"));

       } catch (MalformedURLException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

    }

}

此方式需要知道服務接口,因此,爲了使用服務必須在客戶端創建IHelloWorld 接口

 

public interface IHelloWorld {

   

    public String example(String message);

   

}

2

import java.net.MalformedURLException;

import java.net.URL;

import org.codehaus.xfire.client.Client;

 

public class HelloWorldClient {

    public static void main(String[] args) throws MalformedURLException, Exception {

       Client client = new Client(new

              URL("http://localhost:8080/HelloWorldService/services/HelloWorld?wsdl"));

              Object[] results = client.invoke("example", new Object[] {"Juliet"});

              System.out.println((String) results[0]);

    }

}

3 、根據wsdl 文件生成相應的代碼,再使用,可以採用MyEclipse 幫我們自動生成,具體操作參見相關資料。

生成完成之後,我們就可以像使用本地類一樣使用webservice 服務

package hellows;

 

public class Test {

    public static void main(String[] args) {

       // TODO Auto-generated method stub

       HelloWorldClient hw = new HelloWorldClient();

       HelloWorldPortType hwp = hw.getHelloWorldHttpPort();

       System. out .println(hwp.example( " 遊傳聰 " ));

    }

}

五、 JDK1.6 自帶 webservice 實現

Server:

服務類

package server;

import javax.jws.WebMethod;

import javax.jws.WebService;

import javax.jws.soap.SOAPBinding;

// 定義 Web 服務類和方法

@WebService(targetNamespace = "http://jdk.study.hermit.org/client")

@SOAPBinding(style = SOAPBinding.Style.RPC)

public class Hello {

@WebMethod

public String sayHello(String name) {

return "hello:" + name;

}

}

啓動服務

package server;

 

import javax.xml.ws.Endpoint;

public class StartService {

public static void main(String[] args) {

Endpoint.publish ( "http://localhost:8081/HelloService" , new Hello());

}

}

使用服務

package client;

 

import java.net.MalformedURLException;

import java.net.URL;

import org.codehaus.xfire.XFireFactory;

import org.codehaus.xfire.client.Client;

import org.codehaus.xfire.client.XFireProxyFactory;

import org.codehaus.xfire.service.binding.ObjectServiceFactory;

 

public class HelloClient {

    public static void main(String[] args) throws MalformedURLException, Exception {

       Client client = new Client(new

              URL("http://localhost:8081/HelloService/HelloService?wsdl"));

              Object[] results = client.invoke("sayHello", new Object[] {" 遊傳聰 "});

              System.out.println((String) results[0]);

        

    }

}

運行啓動服務的程序StartService ,即可發佈服務,不需要其他web 容器的支持,實際上自帶有jetty 服務器,發佈服務要確保端口沒被佔用。(相關資源可以在我的資源中找到)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章