1

In Java EE, how to configure container to render null properties using only JAX-RS standard, without annotating or otherwise configuring an actual JAR-RS implementation? And is it possible at all? As an example, lets say I have an end-point and a pojo, and I am returning POJO with some properties set to null. As a result I'd like to see such null properties to be included in JSON too.

POJO:

class Pojo{  
    String name;
    String value;
}

End-point (configured to produce JSON):

Response getPojo(){
    Pojo p = new Pojo();
    p.setName("somename");
    return Response.ok(p).build();
    }

Will produce:

{"name":"somename"} // no value at all

While I'd like to have it like:

{"name":"somename", "value":null} // value is null

Are there any Java EE standard ways to configure such behaviour without messing up with underlying JAX-RS implementation?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
alebu
  • 420
  • 1
  • 5
  • 15
  • 2
    Possible duplicate of [JAX-RS Jersey JSON preserve null using annotations](http://stackoverflow.com/questions/8748537/jax-rs-jersey-json-preserve-null-using-annotations) – Steve C Mar 17 '16 at 03:56

1 Answers1

1

Annotating POJO property with

javax.xml.bind.annotation.XmlElement(nillable=true)

solves the problem. In the example above POJO could have a getter for ID like:

    @XmlElement(nillable=true)
    public Long getId() {
        return id;
    }
alebu
  • 420
  • 1
  • 5
  • 15