how to differentiate the object null and from object that is not given in json by moxy

StackOverflow https://stackoverflow.com/questions/20963179

  •  25-09-2022
  •  | 
  •  

Question

Now I am doing a project that is using restful。In put method, if the client sends json to server 。if object is not given in the json ,we consider the client does not want modify the object ,else the is set as null. wo must modify as null。However now I have a problem ,how to differentiate the object null and from object that is not given in json by moxy。

public class Person{
String id;
String name;
}

the

{
 "id":"1",
 "name":null,

}

same as the

{
 "id":"1"
}

@Blaise Doughan can you help me

Was it helpful?

Solution

By default EclipseLink JAXB (MOXy) will not marshal a mapped field/property with a null value. You can tell MOXy to marshal a null value by mapping it with @XmlElement(nillable=true). I go into more detail about this here:

If you want to differentitate between a default null value and one that is explicitly set to null then you are going to need to track it. Once you are tracking it you can map it with MOXy's @XmlIsSetNullPolicy.

Java Model

import javax.xml.bind.annotation.XmlElement;
import org.eclipse.persistence.oxm.annotations.XmlIsSetNullPolicy;
import org.eclipse.persistence.oxm.annotations.XmlMarshalNullRepresentation;

public class Root {

    @XmlElement
    private String foo;

    @XmlElement(nillable = true)
    private String bar;

    private String baz;
    private boolean bazSet;

    @XmlIsSetNullPolicy(isSetMethodName="isBazSet", nullRepresentationForXml=XmlMarshalNullRepresentation.XSI_NIL)
    public String getBaz() {
        return baz;
    }

    public void setBaz(String baz) {
        this.baz = baz;
        this.bazSet = true;
    }

    public boolean isBazSet() {
        return bazSet;
    }

}

Demo Code

Demo

import java.util.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(2);
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Root.class}, properties);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        Root root = new Root();
        marshaller.marshal(root, System.out);

        root.setBaz(null);
        marshaller.marshal(root, System.out);
    }

}

Output

In the output below we see:

  1. foo is not marshalled out.
  2. bar is marshalled.
  3. baz is not marshalled when it is unset, and is marshalled when it is set even though the value both times is null.
{
   "bar" : null
}
{
   "bar" : null,
   "baz" : null
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top