从Java中的一个WebService返回一个ArrayList [英] Returning an ArrayList from a WebService in Java
问题描述
我有从我的Web服务(Java)的。返回一个ArrayList问题
I am having a problem returning an ArrayList from my web service (Java).
我写了一个测试Web服务和客户端消耗它。所有看似细微的工作 - 这是客户端在调用服务器和服务器接收操作请求
I have written a test web service and client which consumes it. All appears to work fine - that is the client is calling the server and the server receives the operation request.
不过,我已经写了,我希望它返回一个ArrayList的简单方法。
However, I have written a simple method that I want it to return an ArrayList.
我有我的接口定义如下:
I have my interface definition as follows:
@WebService
@SOAPBinding(style = Style.RPC)
public interface ISQLServerConnectionWS {
@WebMethod
ArrayList getSimpleArrayList();
}
我有我的服务器端执行返回的ArrayList
I have my server side implementation to return the ArrayList:
@WebService(endpointInterface="WebServices.ISQLServerConnectionWS")
public class SQLConnectionWSServer
implements ISQLServerConnectionWS {
@Override
public ArrayList getSimpleArrayList() {
ArrayList al = new ArrayList();
al.add( "This" );
al.add( "is" );
al.add( "a" );
al.add( "test" );
return al;
}
}
最后我的客户调用它:
And finally my client call to it:
ArrayList results = server.getSimpleArrayList();
服务器填充数组列表罚款。但是,回到了客户端,ArrayList是空的。它的大小为0。
The server populates the array list fine. However, back at the client side, the ArrayList is empty. It has a size of 0.
如果我在审视我的网址(http://127.0.0.1:9876/myservice-sql?wsdl)为executeSelectSQL的WSDL,它看起来像:
If I examine the WSDL at my URL (http://127.0.0.1:9876/myservice-sql?wsdl) for the executeSelectSQL, it looks like:
<message name="executeSelectSQLResponse">
<part name="return" type="tns:arrayList"/>
</message>
我失去了一些东西明显?
Am I missing something obvious?
修改
不过,如果我有一个Web方法的接口定义为:
However, if I have a web method defines in the interface as:
@WebMethod
String getAString();
和服务器实现:
@Override
public String getAString() {
return "hello there";
}
那么这工作正常 - 你好收到客户端
then this works fine - "hello there" is received on the client.
结束编辑
干杯,
Andez
推荐答案
作为JAXB不能处理的集合作为顶级对象,只能作为Bean的属性使用数组,而不是一个ArrayList的。另外,创建一个bean,并把该ArrayList在里面。
Use an array instead of an ArrayList as JAXB cannot handle collections as top-level objects, only as properties of beans. Alternatively, create a bean and put the ArrayList in it.
请参阅错误: JAXB-223:JAXB不支持集合类作为顶级对象
这篇关于从Java中的一个WebService返回一个ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!