Reading json input on Jersey which can be both nullable and optional

By : Jack
Source: Stackoverflow.com
Question!

I currently have the following REST end-point

@PUT
@Path("/props")
public Response update(Props input) { .. }

and my input parameter is:

@XmlAccessorType(XmlAccessType.FIELD)
class Props {
    public String prop1;
    public String prop2;
}

Client sends input as json string. I want both parameter to be nullable and optional. Example: if a client wants to update prop2 to a null value without updating prop1 it may send:

{"prop2":null}

But at this point I see both input.prop1 = null and input.prop2 = null.

How can I distinguish if client has sent a prop with null value or has not sent it at all?

By : Jack


Answers

All reference type variables are being initialized with null. You may choose some `meaningless' value for initialization.



Isn’t it better to just make it an empty string if you want to set it as null, and null if did not really sent something at all?

By : Debet Wah


This video can help you solving your question :)
By: admin