前面章节:http://www.cnblogs.com/xiehongwei/p/8082337.html 已经开发出了CXF服务端,现在用XFire开发客户端调用CXF服务端,代码如下:
import java.net.MalformedURLException;
import java.net.URL; import org.codehaus.xfire.client.Client; /**
* @filename XfireClient.java
* @author xiehongwei
* @date 2018-1-9 上午10:55:39
*
*/
public class XfireClient { public static void main(String[] args) throws MalformedURLException, Exception {
Client client = new Client(new URL("http://localhost:8082/CxfWSServer/webservice/helloWorld?wsdl"));
Object[] results = client.invoke("sayHi", new Object[] { " World" });
System.out.println(results[0]);
} }
注意:我本地跑了多个tomcat,暂时改了下端口(不影响)
但是运行发现报错,错误如下:
Retrieving document at 'http://localhost:8082/CxfWSServer/webservice/helloWorld?wsdl=HelloWorld.wsdl'.
Exception in thread "main" org.codehaus.xfire.fault.XFireFault: Unmarshalling Error: unexpected element (uri:"http://impl.ws.xie.com/", local:"arg0"). Expected elements are <{}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:382)
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:75)
at org.codehaus.xfire.client.Client.invoke(Client.java:335)
at org.codehaus.xfire.client.Client.invoke(Client.java:349)
at com.test.XfireClient.main(XfireClient.java:18)
通过网上知识搜索发现需要在接口参数上加@WebParam 写入命名空间,接口修改如下:
package com.xie.ws; import javax.jws.WebParam;
import javax.jws.WebService; @WebService
public interface HelloWorld { // String sayHi(String username);
String sayHi(@WebParam(targetNamespace= "http://com.xie.ws/")String username);
}
注意:红色部分为接口所在报名路径
再重新测试成功。