用xfire客户端调用服务端时,传递参数总是null的问题:
1、接口方法返回的复杂类型(如对象类型、数组类型等基本类型之外的类型),特别是对象类型,对象类所在的包结构与客户端该对象所在的包结构一致;
2、服务端的接口类上面加了@WebService,具体方法上加了@WebResult、@WebParam(name="userid",targetNamespace="http://webService/"),接口实现类上也加了@WebService(endpointInterface="webService.CardWebService",portName="CardWebServicePort",serviceName="CardWebService");
3、用xfire编写的客户端如下:
Service service = new ObjectServiceFactory()
.create(CardWebService.class,null,"http://webService/",null);//CardWebService为webservice接口类
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
.newInstance().getXFire());
//XFireProxyFactory factory = new XFireProxyFactory();
String url = "...WebService?wsdl";//wsdl地址
try {
CardWebService cws = (CardWebService) factory.create(service, url);
Test result = cws.queryPersonInfo("", "", "", "", "123456789");//服务端queryPersonInfo方法的返回类型为Test
//...
} catch (MalformedURLException e) {
e.printStackTrace();
}
4、上述代码执行后,服务端接受的参数总是null,这说明客户端将方法的参数传递到服务端时,服务端没办法接受到传递过来的参数。出现此类问题的原因,暂时不清楚(望高手能留言帮我解决,谢谢!)。但是找到了如下的处理办法:
用xfire编写的客户端如下:
public static void main(String[] args) throws Exception {
String url = "...WebService?wsdl";//wsdl地址
Client client = new Client(new URL(url));
Object[] names = client.invoke("queryPersonInfo", new Object[] {"", "", "", "", "123456789"});
System.out.println("names[0]=" + names[0]);// 输出结果:names[0]=[#document: null],但是服务端能接受到客户端传递来的参数
Document xmlTree = (Document) names[0];
anylizeElement(xmlTree);
}
private static void anylizeElement(Document xmlTree) {
Element element = xmlTree.getDocumentElement();
System.out.println(element.getFirstChild().getFirstChild().getNodeName());
System.out.println(element.getFirstChild().getFirstChild().getNodeValue());
NodeList children = element.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node node = children.item(i);
stepThrough(node);
}
}
private static void stepThrough(Node start) {
for (Node child = start.getFirstChild(); child != null; child = child.getNextSibling()) {
if (child instanceof Node)// 去除多余的空白
{
System.out.print("节点名:" + child.getNodeName());
System.out.println("\t节点值:" + child.getNodeValue());
}
if (child != null){
stepThrough(child);
}
}
}
5、执行上述代码后,服务端能接受到客户端传递来的参数,并且客户端也能接受到服务端返回的数据,只不过客户端要将document进行解析。并且编写上述代码后,3中Test类所在的包结构不再要求服务端和客户端保持一致。
6、如果先根据wsdl生成本地客户端,再编写类调用生成的本地客户端的方式来访问服务端,以此种方式访问服务端出现服务端接受参数为null的情况的话,那可以按照2中的步骤进行服务端的配置。