从 Java 中的 WebService 返回一个 ArrayList [英] Returning an ArrayList from a WebService in Java

查看:58
本文介绍了从 Java 中的 WebService 返回一个 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从 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.

我的接口定义如下:

@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.

如果我在我的 URL (http://127.0.0.1:9876/myservice-sql?wsdl) 对于executeSelectSQL,它看起来像:

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";
}

然后这工作正常 - "hello there" 在客户端收到.

then this works fine - "hello there" is received on the client.

推荐答案

使用数组而不是 ArrayList,因为 JAXB 不能将集合作为顶级对象处理,只能作为 bean 的属性.或者,创建一个 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆