Sunday 15 June 2014

java - Play 2.3 Validate Json Request Body -


I was trying to use verification annotation within a Pozo object by sending json to the request body and changing json What I would like to do to Pozo object, to use this service is to return a bad request to this Jason if the object is not valid. Does anyone know about this to go about? I have seen a lot of tutorials that show how to use annotations, but what about just a simple Jason request?

POJO object:

  import play data.validation.Constraints *. Public Class Test Obsoles {{@ Mandatory @ Min (0) Public Integrate Examination; Public integer getTestInt () {return testInt; } Public Zero Settin (integer test) {this.testInt = testInt; }}  

I can probably see each element by parsing Jason and agreeing in that way, but it looks ridiculous ...

  Import model domains; Import play.libs.Json; Import play.mv.BodyParser; Import play.mvc.Controller; Import play.mvc.Result; Import com.fasterxml.jackson.databind.JsonNode; Public Class Test Controller Controller {@ BodyPaster. Off (value = bodypester.jason.class, maxlainth = 10 * 1024) make public static results () {Jasononode Jason = Request). the body (). JSN (); TestObject testObject = Json.fromJson (JSON, TestAbject.class); // object is valid, then only wants to validate the object, // in the bean object // // only when the object is ok, okay, ok ... the return is fine (Jason); }}  

This is what I will do

  @ Bodypaster Off (value = bodypester.jason.class, maxlient = 10 * 1024) make public static results () {Jasononode Jason = Request (). the body (). Asjeson (); // It is not necessary for validity, though to ensure that Jason can be mapped back to PiZo before validating //, you will have to wrap this statement in one stroke so that you can do the mapping Can recover from any error / / test object test object = Jason. From Jason (JSON, TestAbject.class); The form's & lt; TestObject & gt; TestObjectForm = form.form (test object class); The form's & lt; TestObject & gt; TestObjForm = testObjectForm.bind (json); If (testObjForm.hasErrors) {doStuff ()} and {return OK (JSN); }}  

No comments:

Post a Comment