WEBSERVICE XFIRE 服務器端的創建和客戶端調用

開發環境:Windows XP,JDK1.6

開發工具:Myeclipse 6

創建服務器端:

1.新建web service project,命名爲“MyService”

2.連續點擊“NEXT”,選擇“xfire 1.2 core library”、“xfire 1.2 httpclient  library”、“xfire 1.2 jaxb2 library”三個library


3.然後點擊“FINISH”,服務器端項目生成。


4.右鍵單擊“MyService”項目,選擇“NEW”,在“WebServices”下選擇“Web  Service”


5.單擊“NEXT”進入下一步,選擇“Create new Java bean”


6.單擊“NEXT”,在 Web service name 鍵入功能名稱“HelloService”,放在com.guoshuhao.service包下。

同時生成了IHelloService接口和接口實現類HelloServiceImpl


7.點擊“FINISH”完成服務端接口開發,項目業務結構如下


查看IHelloService.java

public interface IHelloService {               
    public String example(String message);        
}
查看並修改HelloServiceImpl.java
public class HelloServiceImpl implements IHelloService { 
    public String example(String message) { 
        return "Hello,My Dear! ---->>>> "+message;
    }
}

services.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
        <service>
                <name>HelloService</name>
                <serviceClass>com.guoshuhao.service.IHelloService</serviceClass>
               <implementationClass>
                        com.guoshuhao.service.HelloServiceImpl
                </implementationClass>
                <style>wrapped</style>
                <use>literal</use> 
               <scope>application</scope>
        </service></beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" 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>
至此,整個webservice的服務端開發完成,讓我們一起測試來看看效果吧。

確認啓動服務器後,打開myeclipse的內置瀏覽器輸入url,url爲

http://localhost:8080/MyService/services/HelloService?wsdl

可以看到以下效果,那麼服務端成功了!


接下來是客戶端對服務端接口調用功能的開發(多種方法)

1.首先建一個Web Project,命名爲“MyClient”

2.向該項目中添加3個jar包,可參考前面服務端創建第2步,完成後如下圖


3.有3種方式創建客戶端服務

第一種:通過MyEclipse的Web Service Client自動創建,個人認爲這種方式不好,不推薦。


第二種:在src下新建com.guoshuhao.client.HelloClient.java,此種方法簡單易用,但不易封裝。

public class HelloClient {
        public static void main(String[] args) throws MalformedURLException, Exception {
        Client client = new Client(new URL("http://localhost:8080/MyService/services/HelloService?wsdl"));
        Object[] results = client.invoke("example", new Object[] {"Guoshuhao"});
        System.out.println((String) results[0]); 
       }
}
在確認服務器啓動後,運行HelloClient.java,在控制檯看到如下輸出:


第三種:在src創建IHelloService接口,同服務端接口相同

package com.guoshuhao.client;public interface IHelloService {
        public String example(String message);
        }
在HelloClient.java中如下編輯
public class HelloClient {
        public static final String SERV_URL= "http://localhost:8080/MyService/services/HelloService";
                public static void main(String[] args) throws MalformedURLException {
                Service serviceModel = new ObjectServiceFactory().create(IHelloService.class);
                IHelloService service = (IHelloService) 
                   new XFireProxyFactory().create(serviceModel, SERV_URL); 
               System.out.println(service.example("狼魂")); 
       }
}
在確認服務器啓動後,運行HelloClient.java,在控制檯看到如下輸出:



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