Can you return an array from a JAX-WS @WebMethod?

2019-02-26 07:07发布

I'm pretty sure you can, but in addition to answering the question in the title, could you also explain the pros, cons, caveats, if any, to doing so? I know that you can't return a List, Set, Collection, Map, or any interface, from a WebMethod (which is stupid, IMO, but I don't know what the design reasons were should I should probably withhold judgment).

Thanks for any advice.

-- LES

2条回答
ゆ 、 Hurt°
2楼-- · 2019-02-26 07:53

You can return arrays, and I have found them useful.

The main reason that collections are a problem is that some languages, such as C, doesn't understand the concept of a collection, so, to remain portable you need to make certain to pass structures that can be represented in the majority of languages.

Besides,I am never a fan of passing collections, as, what comes from the webservice should be fairly static. If you want to add to it, then convert the array to a collection, then pass back an array to the webservice to make changes.

查看更多
霸刀☆藐视天下
3楼-- · 2019-02-26 07:58

James is correct, you can return arrays. You can also return Collection types. The wsdl will define the type as being a list. However, most clients will convert the type into an array (at least that is what I have found C# did with a JAX-WS service).

I used a 3rd party (Apache Axis2) to generate the web service and I came across an odd issue where the wsdl was not being correctly read and the array could not be deserialized correctly by the consumer. It was necessary to create a decorator object, which only contained the array object and have that be returned from the webmethod.

I think that this was just Axis2 not playing well with the .NET web service interpereter, but something to be aware of.

查看更多
登录 后发表回答