java - Spring DataBinder equivalent for Json -
i working on web project using play framework 2.1.0. play supports decent api parsing form data , mapping model beans directly. looks like,
form<employee> form = form.form(employee.class).bindfromrequest(); if (form.haserrors()) { return badrequest(template.render(form)); }
this api validations on fly , capable of handling binding failures, when string not converted integer. form api keeps collection of errors mapped name of property. underlying form api, play using databinder of spring's validation framework doing magic.
i wondering if there similar binding api convert json bean directly, support handling binding failures?
play 2.0 uses jackson internally fails when there binding failures , throws exception. looked @ code , not easy supress these errors.
is there framework can satisfy requirement out of box?
essentially, need framework convert json java bean, can handle binding failures gracefully.
it wonderful if allows me collect them somewhere can generate appropriate validation errors. run custom validations on parsed object using javax.validation apis perform more specific validations once json parsed bean.
i achieved adding custom deserializers in jackson
simpledeserializers deserializers = new simpledeserializers(); deserializers.adddeserializer(integer.class, new myintegerdeserializer(null)); deserializers.adddeserializer(long.class, new mylongdeserializer(null)); objectmapper mapper = new objectmapper().setdeserializerprovider( new stddeserializerprovider().withadditionaldeserializers(deserializers)); mymodel value = mapper.treetovalue(node, mymodel.class);
myintegerdeserializer
, mylongdeserializer
custom deserializers integer
, long
values respectively. these in case exact copy of internal default corresponding deserializer classes additional code gracefully handle numberformatexception
Comments
Post a Comment