Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONProvider returns empty objects such as String

I use JAXB and JSONProvider class for generating response to client in my REST services. 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:

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

Why does it happen? I expect, that it should be: "one":null, "two":null

like image 273
Andrew Avatar asked May 07 '26 04:05

Andrew


1 Answers

According to a JAXB tutorial the absence of a value can be expressed in three different ways:

  • the element is omitted
  • with an empty String using @XmlElement(required = true) annotation
  • with the sepcial xsi:nil attribute using @XmlElement(nillable = true) annotation

Answering strictly your question: JAXB does not support translating empty objects to null.

like image 72
Atticus Avatar answered May 08 '26 19:05

Atticus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!