JAXWS客户端是否会将空集合值和空集合值作为返回值区别开来? [英] Does JAXWS client make difference between an empty collection and a null collection value as returned value?

查看:55
本文介绍了JAXWS客户端是否会将空集合值和空集合值作为返回值区别开来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于JAX-WS依赖JAXB,并且由于我在JAX-B参考实现中观察到了解压缩XML bean的代码,所以我想并没有区别,而且JAXWS客户端始终返回空集合,甚至是Webservice.结果是一个空元素:

Since JAX-WS rely on JAXB, and since I observed the code that unpack the XML bean in JAX-B Reference Implementation, I guess the difference is not made and that a JAXWS client always return an empty collection, even the webservice result was a null element:

public T startPacking(BeanT bean, Accessor<BeanT, T> acc) throws AccessorException {
        T collection = acc.get(bean);
        if(collection==null) {
            collection = ClassFactory.create(implClass);
            if(!acc.isAdapted())
                acc.set(bean,collection);
        }
        collection.clear();
        return collection;
    }

我同意,为了获得最佳的互操作性,服务合同应明确,并避免此类差异,但是看来,我正在调用的JAX-WS服务(托管在具有Jbossws实现的Jboss服务器上)总是返回预期的null空集合(已通过SoapUI测试).

I agree that for best interoperability service contracts should be non ambiguous and avoid such differences, but it seems that the JAX-WS service I'm invoking (hosted on a Jboss server with Jbossws implementation) is always returning as expected a null empty collection (tested with SoapUI).

我使用了wsimport生成的测试代码. 返回元素定义为:

I used for my test code generated by wsimport. Return element is defined as:

@XmlElement(name = "return", nillable = true)
protected List<String> _return;

我什至测试了从以下方法更改Response类的getReturn方法:

I even tested to change the Response class getReturn method from :

public List<String> getReturn() {
    if (_return == null) {
        _return = new ArrayList<String>();
    }
    return this._return;
}

public List<String> getReturn() {
    return this._return;
}

但没有成功.

欢迎您提供有关此问题的任何有用信息/评论!

Any helpful information/comment regarding this problem is welcome !

推荐答案

没有办法在XML中的空集合和null之间产生区别.集合通常被序列化为标签序列(在模式中为xs:sequence),而没有一个封闭的标签表示该集合本身.

There is no way to make a difference between an empty collection and null in the XML. A collection is usually serialized as a sequence of tags (xs:sequence in the schema), without an enclosing tag representing the collection itself.

<item value="item1"/>
<item value="item2"/>

我担心您是否获得null和一个空集合将取决于实现,并且我不会依赖它.如果需要进行区分,可以将集合包装到另一个对象中以生成一个封闭标签(我没有找到生成封闭标签的另一种方法).

I fear that whether you obtain null and an empty collection will be implementation specific, and I would not rely on it. If you need to make the distinction, you can wrap the collection into another object so as to generate an enclosing tag (I didn't found another way to have an enclosing tag be generated).

<items>
  <item value="item1"/>
  <item value="item2"/>
</items>

这篇关于JAXWS客户端是否会将空集合值和空集合值作为返回值区别开来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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