在Eclipse集成開發平臺中利用XFire框架發布Webservice

jopen 9年前發布 | 17K 次閱讀 XFire WEB服務/RPC/SOA

一、正所謂“工欲善其事,必先利其器”,下面就先來配置運行環境吧!

  1. 下載xfire-distribution-1.2.6.zip壓縮包到本地,解壓后將xfire-all-1.2.6.jar和lib文件夾里面的jar放到同一個文件夾xfirejars中。

  2. 下載安裝tomcat并關聯的Eclipse工程中,這個就認為大家都懂,不多說了。

  3. 打開eclipse,點擊File->New->Dynamic Web Project,新建工程FirstWebService

    在Eclipse集成開發平臺中利用XFire框架發布Webservice

  4. 鼠標在FirstWebService上右鍵設置BuildPath->Configure Build Path設置編譯文件classes的存儲位置,具體存儲位置FirstWebService/WebContent/WEB-INF/classes。

    在Eclipse集成開發平臺中利用XFire框架發布Webservice

  5. web.xml文件配置,具體代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>FirstWebService</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
   <servlet>
 <servlet-name>calculateServlet</servlet-name>
 <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>calculateServlet</servlet-name>
 <url-pattern>/services/*</url-pattern>
 </servlet-mapping>
</web-app>

主要是設置<servlet></servlet>標簽中和<servlet-mapping></servlet-mapping>中的代碼。

6.把前面我們下載的xfire壓縮包中復制出來的jar包文件全部復制到FirstWebService/WebContent/WEB-INF/lib文件夾中。


7.接著在FirstWebService/Java Resources/src文件夾中建立包com.calculation.services,其中建立接口類Calculation.java
和接口實現類CalculationImpl.java。

具體代碼如下:

Calculation.java

package com.calculation.services;
public interface Calculation {
 public int add(int a,int b);
 public int sub(int a,int b);
 public int div(int a,int b);
 public int puls(int a,int b);
}

CalculationImpl.java

package com.calculation.services;
public class CalculationImpl implements Calculation {
 public int add(int a ,int b){
  return a+b;
 }
 
 public int sub(int a,int b){
  return a*b;
 }
 
 public int puls(int a,int b){
  return a-b;
 }
 
 public int div(int a,int b) {
  return a/b;
 }
}

 

8.配置services.xml文件。

在FirstWebService/WebContent/META-INF文件夾下建立xfire文件,再在xfire中建立services.xml文件。最后將整個META-INF文件全部剪切到WEB-INF下的classes文件夾中。

services.xml代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
  <service>
    <name>Calculation</name>  
    <namespace>http://com.calculation.services.Calculation</namespace>  
    <serviceClass>com.calculation.services.Calculation</serviceClass>  
    <implementationClass>com.calculation.services.CalculationImpl</implementationClass>
  </service>
</beans>

至此,服務端的配置已經完成,具體的文件圖結構如下:

在Eclipse集成開發平臺中利用XFire框架發布Webservice

然后點擊項目,Run As-》Run On Server

最后在自己的瀏覽器中輸入http://localhost:8080/FirstWebService/services/Calculation?wsdl如果有下面的效果怎么服務端OK!

在Eclipse集成開發平臺中利用XFire框架發布Webservice

二、客戶端編寫,建立FirstWebService/Java Resources/src/com.calculation.services.client

客戶端CalculationClient.java代碼:

package com.calculation.services.client;

import java.net.MalformedURLException;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.calculation.services.*;
public class CalculationClient {
 public  static Calculation calc;
 public  CalculationClient(){};
 
 public static  Calculation getCalculate(String url) throws MalformedURLException {
  if(calc==null) {
   Service srvcModel = new ObjectServiceFactory().create(Calculation.class);
            //創建XFire對象
            XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
            //調用Web服務
            calc = (Calculation) factory.create(srvcModel, url);
  }
  return calc;
 }
 
 public static void main(String[] args) throws MalformedURLException {
    Calculation service = CalculationClient.getCalculate("http://localhost:8080/FirstWebService/services/Calculation");
    int result=service.add(2, 3);
    int result2=service.div(10, 2);
    System.out.println(result);
    System.out.println(result2);
 }
 
 
 
}

運行程序,總體完成!!!!

來自:http://my.oschina.net/u/2356125/blog/477894

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!