Java調用WebService方法總結(6)--XFire調用WebService

XFire是codeHaus組織提供的一個WebService開源框架,目前已被Apache的CXF所取代,已不多有人用了,這裏簡單記錄下其調用WebService使用方法。官網現已不提供下載,能夠到maven倉庫下載,下載地址:https://search.maven.org/artifact/org.codehaus.xfire/xfire-distribution/1.2.6/jar。文中demo所使用到的軟件版本:Java 1.8.0_19一、Xfire 1.2.6。html

一、準備

參考Java調用WebService方法總結(1)--準備工做java

二、調用

2.一、Client方式

public static void client(String param) {
    try {
        Client client = new Client(new URL("http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl"));
        Object[] results = client.invoke("toTraditionalChinese", new Object[]{param});
        System.out.println(results[0]);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

若是服務端是用JAX-WS發佈的,需指定RPC方式(接口類上增長@SOAPBinding(style = SOAPBinding.Style.RPC));調用本地的服務:web

/**
 * JAX-WS發佈的服務端需指定RPC方式
 * @param param
 */
public static void client2(String param) {
    try {
        Client client = new Client(new URL("http://10.40.103.48:9006/zsywservice/TestService?wsdl"));
        Object[] results = client.invoke("hello", new Object[]{param});
        System.out.println(results[0]);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

2.二、ServiceFactory方式

這種方式須要把接口類拷貝到客戶端。測試時調用本地服務時參數傳不進去,老是null;不知爲什麼;有了解的朋友請指正。框架

/**
 * 調用本地服務時參數傳不進去,老是null
 * @param param
 */
public static void factory(String param) {
    try {
        Service serviceModel = new ObjectServiceFactory().create(ITestService.class, null, "http://ws.zsyw.inspur.com/", null);
        XFireProxyFactory factory = new XFireProxyFactory();
        ITestService testService = (ITestService) factory.create(serviceModel, "http://10.40.103.48:9006/zsywservice/TestService?wsdl");
        String result = testService.hello(param);
        System.out.println(result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

2.三、完整代碼

package com.inspur.ws;

import java.net.URL;

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 com.inspur.zsyw.ws.ITestService;

/**
 * 
 * xfire調用WebService
 *
 */
public class Xfire {
    public static void client(String param) {
        try {
            Client client = new Client(new URL("http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl"));
            Object[] results = client.invoke("toTraditionalChinese", new Object[]{param});
            System.out.println(results[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * JAX-WS發佈的服務端需指定rpc方式
     * @param param
     */
    public static void client2(String param) {
        try {
            Client client = new Client(new URL("http://10.40.103.48:9006/zsywservice/TestService?wsdl"));
            Object[] results = client.invoke("hello", new Object[]{param});
            System.out.println(results[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 調用本地服務時參數傳不進去,老是null
     * @param param
     */
    public static void factory(String param) {
        try {
            Service serviceModel = new ObjectServiceFactory().create(ITestService.class, null, "http://ws.zsyw.inspur.com/", null);
            XFireProxyFactory factory = new XFireProxyFactory();
            ITestService testService = (ITestService) factory.create(serviceModel, "http://10.40.103.48:9006/zsywservice/TestService?wsdl");
            String result = testService.hello(param);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        client("大學");
        client2("大學");
        factory("大學");//hello,null
    }
}
View Code
相關文章
相關標籤/搜索