繁体   English   中英

JAX-WS 返回空元素

[英]JAX-WS return empty element

我使用 JAX-WS 并返回产品实体列表。

产品具有以下属性:

  • ID
  • 姓名
  • 描述
  • 等等。

描述具有字符串或空值。 我调试的产品列表和描述值是有效的。 当 description 为空时,描述元素不包含在 SOAP 响应中。 我想在带有 NULL 值的 SOAP 响应中使用这个元素。

这是响应的转储:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:getProductsResponse xmlns:ns2="http://blabla.com/">
         <return>
            <idProduct>1</idProduct>
            <name>name</name>
            <description>some desc</description>
         </return>
         <return>
            <idProduct>2</idProduct>
            <name>name</name>
         </return>
      </ns2:getProductsResponse>
   </S:Body>
</S:Envelope>

我想要:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:getProductsResponse xmlns:ns2="http://blabla.com/">
         <return>
            <idProduct>1</idProduct>
            <name>name</name>
            <description>some desc</description>
         </return>
         <return>
            <idProduct>2</idProduct>
            <name>name</name>
            <description>NULL</description>
         </return>
      </ns2:getProductsResponse>
   </S:Body>
</S:Envelope>

这是我的网络方法:

@WebMethod(operationName = "getProducts")
public List<ProductDTO> getProducts(@WebParam(name = "idCompany") int idCompany) {
        ProductHelper helper = new ProductHelper();
        // this list was debuged and it is correct
        List<ProductDTO> products = helper.getAll(idCompany);
        return products;
}

我使用 JAX-WS RI 2.2-hudson-740

问题是计算机无法判断您希望将null序列化为字符串“ NULL ”; 这对计算机来说一点也不明显。 (它也不会对您的客户很明显。)因此,计算机以默认的 JAXB 方式处理null :它省略了元素。

如果您希望元素在那里,则需要在ProductDTO上使用 JAXB 注释来说明情况就是如此。 您可能还想让元素为空; 虽然它不会完全转换为您所说的您正在寻找的内容,但它至少应该做正确的事情(并且您的客户应该能够应对)。

这将在description字段(或getDescription()方法)上放置这样的注释:

@XmlElement(required=true, nillable=true)

执行此操作的另一种方法是添加一个getDescription()方法,该方法返回字符串"NULL" ,否则返回null (否则返回真实值)。 这样做的问题是它可能会混淆您的数据库绑定层; 通过明智地注释元素(并使用更好的序列化)来正确地完成工作会减少很多痛苦。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM