天天看點

xfire用戶端擷取xcf服務端接口解析問題2

1、xfire接口代碼

import org.codehaus.xfire.client.Client;
import org.w3c.dom.Document;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.net.URL;

public class Test {
    private static String WSDL = "";

    static{
        WSDL = "http://localhost:8080/userWebService?wsdl";
    }

    public static void main(String[] args){
        getAllUsers();
    }

    public static void getAllUsers() {
        String result = "";
        try {
            Client client = new Client(new URL(WSDL));

            Object[] results = client.invoke("getUserByName", new Object[]{"李四"});

         //   Object[] results = client.invoke("getAllUsers", new Object[]{});

            Document d = (Document)results[0];

            System.out.println(toFormatedXML(d));

            System.out.println(result);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String toFormatedXML(Document doc) throws Exception {
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transFormer = transFactory.newTransformer();
        transFormer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        DOMSource domSource = new DOMSource(doc);
        StringWriter sw = new StringWriter();
        StreamResult xmlResult = new StreamResult(sw);
        transFormer.transform(domSource, xmlResult);
        return sw.toString();

    }
}
           

2、報錯資訊

org.codehaus.xfire.fault.XFireFault: Unmarshalling Error: 意外的元素 (uri:"http://xxx.com/", local:"arg0")。所需元素為<{}arg0> 
	at org.codehaus.xfire.fault.Soap11FaultSerializer.readMessage(Soap11FaultSerializer.java:31)
	at org.codehaus.xfire.fault.SoapFaultSerializer.readMessage(SoapFaultSerializer.java:28)
	at org.codehaus.xfire.soap.handler.ReadHeadersHandler.checkForFault(ReadHeadersHandler.java:111)
	at org.codehaus.xfire.soap.handler.ReadHeadersHandler.invoke(ReadHeadersHandler.java:67)
	at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
	at org.codehaus.xfire.client.Client.onReceive(Client.java:406)
	at org.codehaus.xfire.transport.http.HttpChannel.sendViaClient(HttpChannel.java:139)
	at org.codehaus.xfire.transport.http.HttpChannel.send(HttpChannel.java:48)
	at org.codehaus.xfire.handler.OutMessageSender.invoke(OutMessageSender.java:26)
	at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
	at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:79)
	at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:114)
	at org.codehaus.xfire.client.Client.invoke(Client.java:336)
	at org.codehaus.xfire.client.Client.invoke(Client.java:368)
	at com.chinaoly.Test.getAllUsers(Test.java:33)
	at com.chinaoly.Test.main(Test.java:22)
           

3、解決辦法

需要修改服務段接口代碼,增加@WebParam參數部分内容

import javax.jws.WebParam;
import javax.jws.WebService;
import java.util.List;

@WebService
public interface IUserWebService {
    
    User getUserByName(@WebParam(name="userName",targetNamespace = "http://xx.com/") String userName);

    List<User> getAllUsers();

}
           

4、解決辦法2

如果服務端是别人提供的,可能不好要求别人改代碼,是以隻能修改自己的用戶端代碼,改成cfx方式

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.eclipse.jetty.util.ajax.JSON;
import java.net.URLDecoder;

public class TestCFX {
    public static void main(String args[]){

        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8080/userWebService?wsdl");
        String result = "";

        try{
            Object[] objects = client.invoke("getUserByName","李四");
            result = URLDecoder.decode(JSON.toString(objects),"utf-8");
            System.out.println(result);

            objects = client.invoke("getAllUsers");
            result = URLDecoder.decode(JSON.toString(objects),"utf-8");
            System.out.println(result);

        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
           

5、嘗試kettle的web service元件

流程:生成查詢參數,查詢之後把結果寫到文本檔案

xfire用戶端擷取xcf服務端接口解析問題2

webservice查詢配置:

xfire用戶端擷取xcf服務端接口解析問題2

剛開始報錯,錯誤資訊如下:

xfire用戶端擷取xcf服務端接口解析問題2

沒拷貝全的資訊如下:

2019/10/23 09:52:40 - Web 服務查詢.0 - Response body: 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body><soap:Fault><faultcode>soap:Server</faultcode>
<faultstring>The given SOAPAction http://chinaoly.com/getAllUsers does not match an operation.</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
           

查找的解決辦法,也是修改服務端的代碼,添加 @WebMethod部分的資訊

import javax.jws.WebMethod;
import javax.jws.WebService;
import java.util.List;

@WebService
public interface IUserWebService {
    //@WebParam(name="userName",targetNamespace = "http://xx.com/")
    @WebMethod(action="http://xx.com/getUserByName")
    User getUserByName( String userName);

    @WebMethod(action="http://xx.com/getAllUsers")
    List<User> getAllUsers();

}
           

繼續閱讀