简体   繁体   English

JSONProvider返回空对象,例如String

[英]JSONProvider returns empty objects such as String

I use JAXB and JSONProvider class for generating response to client in my REST services. 我使用JAXBJSONProvider类在REST服务中生成对客户端的响应。 And I have found one interesting thing. 我发现了一件有趣的事情。 I have the mapping: 我有映射:

@XmlRootElement(name = "fooRoot")
@XmlAccessorType(XmlAccessType.FIELD)
public class WSBar {
     private WSFooTO foos;
//...
}

@XmlRootElement(name = "foos")
@XmlAccessorType(XmlAccessType.FIELD)
public class WSFooTO {
    private WSDateTO one;
    private WSDateTO two;
    private WSDateTO three;
    private WSDateTO four;
//....
}

When I set to the response empty objects, I mean: just new WSDateTO() and new WSFooTO () After the, I recive answer like this: 当我将响应设置为空对象时,我的意思是:只是new WSDateTO()new WSFooTO ()之后,我会这样回答:

{"fooRoot":{
    "foos":{
        "one":"",
        "two":"",
        "three":"",
        "four":""
    }
}}

Why does it happen? 为什么会发生? I expect, that it should be: "one":null, "two":null 我希望它应该是:“ one”:null,“ two”:null

According to a JAXB tutorial the absence of a value can be expressed in three different ways: 根据JAXB 教程 ,值的缺失可以用三种不同的方式表示:

  • the element is omitted 元素被省略
  • with an empty String using @XmlElement(required = true) annotation 使用@XmlElement(required = true)注释的空字符串
  • with the sepcial xsi:nil attribute using @XmlElement(nillable = true) annotation 使用@XmlElement(nillable = true)注释的单独xsi:nil属性

Answering strictly your question: JAXB does not support translating empty objects to null . 严格回答您的问题:JAXB不支持将空对象转换为null

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

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