i'm using jackson 2.7.0. , latest jersey json on rest api handles db communication hibernate 5+.
i don't know how verify incoming json if there missing properties in it. impossible perform checking on primitive type if null. use primitive types because of performance hit. best practice handle such problem?
when receive json below, ok:
{"vehicle":{"id":1},"distance":1000,"quantity":2000}
but when receive json like:
{"vehicle":{"id":1},"quantity":2000}
then distance set default value 0
.
my entity like
public class consumption{ private int id; private double quantity; private double distance; @jsoncreator(mode = jsoncreator.mode.properties) public consumption( @jsonproperty(value = "quantity", required = true)double quantity, @jsonproperty(value = "distance", required = true)double distance, @jsonproperty(value = "vehicle", required = false)vehicle vehicle) {...
and rest method:
@post @path("/setconsumption/") public response setconsumption(@valid consumption consum){...
tried solution #1
i have tried, set values default value of -1
, check if less 0, problem remains boolean
values, can not set "neutral" value.
how handle such problem missing property in json?
tried solution #2
as can see have used @jsonproperty(value = "quantity", required = true)
in constructor. new feature jackson 2.7.0.+, handles type of problem. exception:
missing required creator property 'distance' (index 1) @ [source:org.glassfish.jersey.message.internal.readerinterceptorexecutor$uncloseableinputstream@1b72cb6; line: 1, column: 181]
this exception returned user before json reaches code. can not catch exception. have custom responses httpcode , errormessage inform user them. problem don't know how catch exception , return user in custom form text.
searching solution
i have either catch exception of new jackson feature or try validate json. not use class integer instead of primitive data type int, because of performance. if possible not write custom json deserializer 60+ classes have in project.
if other solution jackson or jersey supports types of handling of missing properties, feel free comment.
tdlr - how check if variable of primitive data type (int, double, boolean,...) in incoming json or not, without manually writting deserializers each of 60+ classes in project. java not allow check types null
.
suggestion #1
trying register exceptionmapper jsonmappingexception , see if overrides 1 registered jersey.
my code (still getting default "missing property ..." exception - problem not solved):
@provider public class jacksonnopropertymapper implements exceptionmapper<jsonmappingexception> { @override public response toresponse(jsonmappingexception e) { return response.status(999).entity("override test").type(mediatype.application_json).build(); }
}
the easiest way approach use object counter parts on fields optional, shouldn't affect performance wise, , allow null check fields.
public class consumption { private int id; private double quantity; private double distance; public boolean isdistanceset() { return distance != null; }
Comments
Post a Comment