XFire WebService使用執行個體

來源:互聯網
上載者:User

標籤:xfire   webservice   

幾年前搞過,當時只是匆匆的用。相隔幾年之後再用時有些模糊了,參考了別人的例子實現了,特此總結下。

一、要使用先下載:http://xfire.codehaus.org/Download

二、先說服務端:

       1、建立JavaWeb項目,將jar包拷進webroot/lib下面;(xfire-all-1.2.6.jar和xfire-distribution-1.2.6\xfire-1.2.6\lib下面的所以jar都拷)

        2、在WebRoot/META-INF下面建立檔案夾xfire;

       3、在xfire檔案夾下面建立services.xml檔案

    內容如下:

<?xml version="1.0" encoding="UTF-8"?><!-- <beans>    <service xmlns="http://xfire.codehaus.org/config/1.0">      <name>Hello</name>      <namespace>http://com.qh.helloWorld/Hello</namespace>      <serviceClass>com.qh.helloWorld.Hello</serviceClass>    </service></beans> --><beans xmlns="http://xfire.codehaus.org/config/1.0">  <service>    <name>KingTeamService</name>    <namespace>http://com.yao.xfire.service/KingTeamService</namespace>    <serviceClass>com.yao.xfire.service.MathService</serviceClass>    <implementationClass>com.yao.xfire.service.MathServiceImpl</implementationClass>  </service></beans>

4、在web.xml檔案裡面添加service攔截

內容如下:

<servlet><servlet-name>xfireServlet</servlet-name><servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class></servlet><servlet-mapping><servlet-name>xfireServlet</servlet-name><url-pattern>/services/*</url-pattern></servlet-mapping>

5、建立KingTeamService.java 介面

內容如下:

package com.yao.xfire.service;public interface KingTeamService {public int add(int a,int b);}

6、建立KingTeamService.java介面的實作類別

內容如下:

package com.yao.xfire.service;import com.sun.org.apache.commons.logging.Log;import com.sun.org.apache.commons.logging.LogFactory;public class MathServiceImpl implements KingTeamService{private final Log log = LogFactory.getLog(MathServiceImpl.class);@Override    public int add(int a, int b) {        log.info("invoke method add.");        return a + b;    }}

三、調用服務的用戶端:

1、MathServiceClient.java

package com.yao.xfire.client;import java.net.MalformedURLException;import org.codehaus.xfire.XFireFactory;import org.codehaus.xfire.client.Client;import org.codehaus.xfire.client.XFireProxyFactory;import org.codehaus.xfire.service.Service;import org.codehaus.xfire.service.binding.ObjectServiceFactory;import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;import com.yao.xfire.service.KingTeamService;public class MathServiceClient {    private static KingTeamService mathService;        private MathServiceClient(){}         public static KingTeamService getMathService(String serviceUrl) throws MalformedURLException{        if(mathService == null){            //建立服務            Service srvcModel = new ObjectServiceFactory().create(KingTeamService.class);            //建立XFire對象            XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());            //調用Web服務            mathService = (KingTeamService) factory.create(srvcModel, serviceUrl);                         //設定用戶端調用的屬性            Client client = Client.getInstance(mathService);            client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "300");            client.setProperty(CommonsHttpMessageSender.DISABLE_KEEP_ALIVE, "true");            client.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE, "true");            //如果需要設定代理            //client.setProperty(CommonsHttpMessageSender.HTTP_PROXY_HOST, "10.3.1.6" );            //client.setProperty(CommonsHttpMessageSender.HTTP_PROXY_PORT, "8080");        }                 return mathService;    }         public static void main(String[] args) throws MalformedURLException{        KingTeamService service = MathServiceClient.getMathService("http://localhost:80/xfireD/services/MathService");        int result = service.add(1, 14);        System.out.println("get the result: " + result);    }}


然後將項目放進web伺服器裡面,運行。然後在瀏覽器裡面輸入訪問地址進行測試:http://localhost/xfireD/services/KingTeamService?wsdl

運行用戶端即可得到計算的結果。

例子雖然簡單,但卻將使用的過程說的清楚明白。以後可以參考著實現了,哈哈。


 

XFire WebService使用執行個體

聯繫我們

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