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

1、正所謂「工欲善其事,必先利其器」,下面就先來配置運行環境吧!html

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

  2. 下載安裝tomcat並關聯的Eclipse工程中,這個就認爲你們都懂,很少說了。web

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

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

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

<?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>中的代碼。eclipse

6.把前面咱們下載的xfire壓縮包中複製出來的jar包文件所有複製到FirstWebService/WebContent/WEB-INF/lib文件夾中。jsp

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

具體代碼以下:url

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>

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

而後點擊項目,Run As-》Run On Server

最後在本身的瀏覽器中輸入http://localhost:8080/FirstWebService/services/Calculation?wsdl若是有下面的效果怎麼服務端OK!

2、客戶端編寫,創建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);
 }
 
 
 
}

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

相關文章
相關標籤/搜索