使用axis 调用webservice时怎么处理返回返回数组

IT东 2009-07-16 10:14:23
我最近开始接触webservice ,使用axis调用webservice时如果返回的值为字符串或者schema.xml都不会出错,但是如果返回的是数组时就会出如下异常:
Exception:
org.xml.sax.SAXException: No deserializer for {http://www.w3.org/2001/XMLSchema}anyType
at org.apache.axis.encoding.DeserializerImpl.onStartElement(DeserializerImpl.java:453)
at org.apache.axis.encoding.DeserializerImpl.startElement(DeserializerImpl.java:393)
at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1048)
at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:165)
at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141)
at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:236)
at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)

我在网上查了很久的资料,很少有对返回字符串数组的处理,基本都是直接返回字符串,都是千篇一律,郁闷啊,那位高手帮帮忙啊!!!
我的主要代码为:

package webservice;

import java.net.URL;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.*;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

public class Test {

public static void main(String[] args) {
Service service = new Service();
Call call;
try {
call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"));
call.setOperationName(new QName("http://WebXml.com.cn/","getWeatherbyCityName"));
call.addParameter(new QName("http://WebXml.com.cn/", "theCityName"),XMLType.XSD_STRING,ParameterMode.IN);
call.setReturnType(XMLType.SOAP_ARRAY);
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://WebXml.com.cn/getWeatherbyCityName");
String[] result = (String[])call.invoke(new Object[]{"昆明"});
} catch (Exception e) {
e.printStackTrace();
}

}
}

...全文
751 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuhuang007 2010-12-15
  • 打赏
  • 举报
回复
首先纠正一下楼上的笔误,不是序列化,而是反序列化

你用的是比较老版的jaxrpc客户端调用服务,因为早期的web服务使用的是SOAP编码,而现在使用的是XML Schema类型系统。这两个类型提供有一个不同点,就是数组的表示方法不一样。
比如 int[] a={1,2,3}
用soap编码表示
<a>
<item>1</item>
<item>2</item>
<item>3</item>
</a>
如果使用XML Schema编码
<a>1</a>
<a>2</a>
<a>3</a>
而Axis也是用XML Schema编码的
call.setReturnType(XMLType.SOAP_ARRAY);这是你设置反序列化的数据类型
你使用SOAP编码当然没有办法反序列化出正确的数组了。

我不知道有没有说明白
=========================我是分隔线==============================
调用web服务有很多方法,也不受语言和环境的限制
建议你使用比较新的jaxws调用服务,具体你查看相关资料
或者使用Axis提供的客户端。下面是Axis的客户端。你得下载相应的jar



RPCServiceClient serviceClient = new RPCServiceClient();

Options options = serviceClient.getOptions();

EndpointReference targetEPR = new EndpointReference(
"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
options.setTo(targetEPR);

// /////////////////////////////////////////////////////////////////////

/*
* Creates an Entry and stores it in the AddressBook.
*/

// QName of the target method
QName opGetWeather = new QName("http://WebXml.com.cn/", "getWeatherbyCityName");
Object[] opGetWeatherArgs = new Object[] { "昆明"};
Class[] returnTypes = new Class[] { String[].class };
Object[] response = serviceClient.invokeBlocking(opGetWeather ,
opGetWeatherArgs , returnTypes);
String[] result=(String[])response[0];
cds27 2009-12-01
  • 打赏
  • 举报
回复
你返回的类型不可以序列化。
你必须返回一个基本数据类型,或者基本数据类型数组。
而不能直接返回复杂类型的数组。
检查WSDL中的类型是否正确。
hexhero 2009-11-25
  • 打赏
  • 举报
回复
自己写部署文件 deploy.wsdd
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="ISealManager" provider="java:RPC" >
<parameter name="className" value="com.lnseal.soap.service.ISealManager" />
<parameter name="allowedMethods" value="*"/>

<beanMapping qname="ns11:IOperator"
xmlns:ns11="LnSealManager"
languageSpecificType="java:com.lnseal.soap.sto.IOperator"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>

<typeMapping
xmlns:ns54="LnSealManager"
qname="ns54:ArrayOfOperator"
type="java:com.lnseal.soap.sto.IOperator[]"
serializer="org.apache.axis.encoding.ser.ArraySerializerFactory"
deserializer="org.apache.axis.encoding.ser.ArrayDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
/>

</service>
</deployment>


Dos 下输入如下命令生成服务发布文件 server-config.wsdd
java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient deploy.wsdd

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧