Web服务响应是否应包含空值? [英] Should a web service response include empty values?

查看:59
本文介绍了Web服务响应是否应包含空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用Web服务时,我得到XML格式的动态响应。

When invoking a web service I get a dynamic response in XML format.

因此响应可能是:

<response>
<test1>test1</test1>
<test2>test1</test2>
<test3>test1</test3>
<test4>test1</test4>
</response>

或:

<response>
<test1>test1</test1>
<test2>test1</test2>
</response>

但我认为响应应该是静态的,以便从XML中正确地解组Java类。

But I think the response should be static in order for the Java class to be unmarshalled correctly from the XML.

所以而不是

<response>
<test1>test1</test1>
<test2>test1</test2>
</response>

这应该是:

<response>
<test1>test1</test1>
<test2>test1</test2>
<test3></test3>
<test4></test4>
</response>

这意味着我现在可以处理响应并检查缺失的数据。

This means I can now handle the response and check for missing data.

我的想法是否正确?

推荐答案

默认空表示



默认情况下,JAXB(JSR-222)实现会将属性视为可选元素。因此,空值表示为文档中不存在的元素。

Default Null Representation

By default a JAXB (JSR-222) implementation will treat a property as an optional element. As such null values are represented as the element being absent from the document.

或者你可以通过包含 xsi表示null: nil =true属性。这可以通过使用 @XmlElement(nillable = true)注释您的财产来实现。

Alternatively you have null represented by including the xsi:nil="true" attribute on it. This is achieved by annotating your property with @XmlElement(nillable=true).

<date xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>



无效的空表示



空元素不是null的有效表示。它将被视为一个空字符串,对所有非String类型都是无效的。

Invalid Null Representation

An empty element is not a valid representation for null. It will be treated as an empty String which will be invalid for all non-String types.

<date/>



更多信息




  • http://blog.bdoughan .com / 2012/04 / binding-to-json-xml-handling-null.html

  • For More Information

    • http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html

    • SO test1 test1是
      a有效响应但是字段test3& test4将被设置为null?

      SO test1 test1 is a valid response but the fields test3 & test4 will be set to null ?

      对于与缺席节点相对应的字段/属性,没有做任何事情。它们将保留默认值,默认情况下初始化为 null

      What happens is that nothing is done for the fields/properties that correspond to absent nodes. They will keep there default values, which are by default initialized to null.

      Java模型(Root) )

      在下面的模型类中,我初始化了字段,使其值不是 null

      In the model class below I have initialed the fields to have values that are not null.

      import javax.xml.bind.annotation.*;
      
      @XmlRootElement
      public class Root {
      
          @XmlElement
          String foo = "Hello";
      
          String bar = "World";
      
          public String getBar() {
              return bar;
          }
      
          public void setBar(String bar) {
              this.bar = bar;
          }
      
      }
      

      演示

      正在编组的文件< root /> 没有与映射字段对应的任何元素/模型类中的属性。

      The document being marshalled <root/> does not have any elements corresponding to the mapped fields/properties in the model class.

      import java.io.StringReader;
      import javax.xml.bind.*;
      
      public class Demo {
      
          public static void main(String[] args) throws Exception {
              JAXBContext jc = JAXBContext.newInstance(Root.class);
      
              Unmarshaller unmarshaller = jc.createUnmarshaller();
              StringReader xml = new StringReader("<root/>");
              Root root = (Root) unmarshaller.unmarshal(xml);
      
              System.out.println(root.foo);
              System.out.println(root.bar);
          }
      
      }
      

      输出

      我们看到输出的默认值。这表明没有为缺席节点执行设置操作。

      We see that the default values were output. This shows that a set operation was not performed for the absent nodes.

      Hello
      World
      

      这篇关于Web服务响应是否应包含空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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