XFire 是 CXF的前身,已经停止更新多年,如非必要,请勿浪费时间。
CXF服务端
示例
import javax.jws.WebParam;import javax.jws.WebService;@WebServicepublic interface WsTest {String do(@WebParam(targetNamespace= "http://com.dzero.ws/")String something);}
注意
入参的注解 @WebParam(targetNamespace= "http://com.dzero.ws/") 必须添加,如果不添加该注解以及其命名空间,XFire客户端调用时会报如下相关错误:
org.codehaus.xfire.fault.XFireFault: Unmarshalling Error: unexpected element (uri:"http://com.dzero.ws/", local:"arg0"). Expected elements are <{}arg0>
targetNamespace 的具体值可在wsdl的xml页面内容中找到(一般为package的倒叙)
XFire客户端
正常调用
import java.net.MalformedURLException;import java.net.URL;import org.codehaus.xfire.client.Client;public class XfireClient {public static void main(String[] args) throws MalformedURLException, Exception {String urlStr="http://localhost:8080/CxfWSServer/webservice/WsTest?wsdl";Client client = new Client(new URL(urlStr));Object[] results = client.invoke("do", new Object[] { " shopping" });System.out.println(results[0]);}}
添加Http头信息
import java.net.MalformedURLException;import java.net.URL;import org.codehaus.xfire.client.Client;import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;public class XfireClient {public static void main(String[] args) throws MalformedURLException, Exception {String urlStr="http://localhost:8080/CxfWSServer/webservice/WsTest?wsdl";Client client = new Client(new URL(urlStr));Map m = new HashMap();m.put("userName", "dzero");client.setProperty(CommonsHttpMessageSender.HTTP_HEADERS, m);Object[] results = client.invoke("do", new Object[] { " shopping" });System.out.println(results[0]);}}
