从 Java 中的 WebService 返回一个 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6609770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:13:48  来源:igfitidea点击:

Returning an ArrayList from a WebService in Java

javaservicearraylist

提问by Andez

I am having a problem returning an ArrayList from my web service (Java).

我在从 Web 服务 (Java) 返回 ArrayList 时遇到问题。

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.

我编写了一个测试 Web 服务和使用它的客户端。一切似乎都工作正常 - 即客户端正在调用服务器并且服务器接收操作请求。

However, I have written a simple method that I want it to return an ArrayList.

但是,我编写了一个简单的方法,希望它返回一个 ArrayList。

I have my interface definition as follows:

我的接口定义如下:

@WebService
@SOAPBinding(style = Style.RPC)
public interface ISQLServerConnectionWS {

    @WebMethod
    ArrayList getSimpleArrayList();
}

I have my server side implementation to return the ArrayList:

我有我的服务器端实现来返回 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();

The server populates the array list fine. However, back at the client side, the ArrayList is empty. It has a size of 0.

服务器很好地填充数组列表。但是,回到客户端,ArrayList 是空的。它的大小为 0。

If I examine the WSDL at my URL (http://127.0.0.1:9876/myservice-sql?wsdl) for the executeSelectSQL, it looks like:

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

<message name="executeSelectSQLResponse">
    <part name="return" type="tns:arrayList"/>
</message>

Am I missing something obvious?

我错过了一些明显的东西吗?

Edit:

编辑:

However, if I have a web method defines in the interface as:

但是,如果我在界面中有一个 Web 方法定义为:

@WebMethod
String getAString();

and the server implementation:

和服务器实现:

@Override
public String getAString() {
    return "hello there";
}

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

那么这工作正常 -"hello there"在客户端收到。

采纳答案by dogbane

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.

使用数组而不是 ArrayList,因为 JAXB 不能将集合作为顶级对象处理,只能作为 bean 的属性。或者,创建一个 bean 并将 ArrayList 放入其中。

See bug: JAXB-223: JAXB doesn't support collection classes as top level objects

请参阅错误:JAXB-223:JAXB 不支持将集合类作为顶级对象

回答by BOSS

I will suggest create separate pojo class where declare a vaiable

我会建议创建单独的 pojo 类,在其中声明一个 vaiable

private ArrayList ListData;

create setter/getter method and use the POJO class in your main class to set the arraylist. At same time the Operation getSimpleArrayList change the return type to that of POJO type. Accordingly change the wsdl file too.

创建 setter/getter 方法并使用主类中的 POJO 类来设置数组列表。同时操作 getSimpleArrayList 将返回类型更改为 POJO 类型。相应地也更改 wsdl 文件。