Convert JSON to JAVA Object through play.data.Form -



Convert JSON to JAVA Object through play.data.Form -

i have no thought problem. have json object assist in value input through post (using play framework) structures this:

{ "start_absolute": 1403185486254, "end_absolute": 1403185486254, "metrics": [ { "name": "parts", "tags": [ { "key":"machine", "value":"10" } ], "sampling": { "value": 1, "unit": "minutes" } } ] }

and in server side seek process info this:

form<queryform> queryform = form.form(queryform.class).bindfromrequest(); if (queryform.haserrors()) { homecoming badrequest(queryform.errorsasjson()); } queryform queryinfo = queryform.get();

the classes used define json object in java

public class queryform { private date start_absolute; private date end_absolute; private list<metricform> metrics= lists.newarraylist(); public date getstart_absolute() { homecoming start_absolute; } public void setstart_absolute(long start_absolute) { this.start_absolute = new date(start_absolute); } public date getend_absolute() { homecoming end_absolute; } public void setend_absolute(long end_absolute) { this.end_absolute =new date(end_absolute); } public list<metricform> getmetrics() { homecoming metrics; } public void setmetrics(list<metricform> metrics) { this.metrics = metrics; } @override public string tostring() { homecoming "queryform[start_absolute="+start_absolute+", end_absolute="+end_absolute+", metrics="+metrics+"]"; }

.

public class metricform { private string name; private list<tag> tags= lists.newarraylist(); private relativetimeform sampling= new relativetimeform(1,"milliseconds"); public string getname() { homecoming name; } public void setname(string name) { this.name = name; } public list<tag> gettags() { homecoming tags; } public void settags(list<tag> tags) { this.tags.addall(tags); } public relativetimeform getsampling() { homecoming sampling; } public void setsampling(int val, string unit) { this.sampling.setunit(unit); this.sampling.setval(val); } @override public string tostring() { homecoming "metricform[name="+name+", tags="+tags+",sampling="+sampling+"]"; }

.

public class tag { private string key; private string value; public tag(string key, string value) { this.key = key; this.value = value; } public string getkey() { homecoming key; } public void setkey(string key) { this.key = key; } public string getvalue() { homecoming value; } public void setvalue(string value) { this.value = value; } @override public string tostring() { homecoming string.format( "tag[key=%s, value='%s']", key, value ); } }

.

public class relativetimeform { private int value; private string unit; public relativetimeform (int val, string unit){ this.setvalue(val); this.setunit(unit); } public int getvalue() { homecoming value; } public void setvalue(int value) { this.value = value; } public string getunit() { homecoming unit; } public void setunit(string unit) { this.unit = unit; } @override public string tostring() { homecoming "relativetimeform[value="+value+", unit="+unit+"]"; }

i'm not experienced working play.data.form in previous ocasions simpler objects (using primitive info types , string) worked fine when tried post play application (header: content-type = application/json; charset=utf-8) occurs internal server error:

play.api.application$$anon$1: execution exception [[invalidpropertyexception: invalid property 'metrics[0].tags[0]' of bean class [model.queryform]: illegal effort property 'tags' threw exception; nested exception org.springframework.beans.nullvalueinnestedpathexception: invalid property 'metrics[0].tags' of bean class [model.queryform]: not instatiate property path: java.lang:instatiationexception: [model.tag]

any 1 can help this? can´t figure out do. complex of object convert json? need add together json annotations (not using them)?

i have no clue how prepare this

update: narrowed downwards tag class. reason i'm not able instatiate tag through json. tried setting single tag instance in same level dates , gives same error (it work sampling)

resolved:

spring binding exception when form submitted

the reply in post above. have set empty construtor in tag class work.

public tag(){ }

the exception means, 1 or more attributes aren't instantiated.

private list<metricform> metrics = new ...; private list<tag> tags = new ...; private relativetimeform sampling = new ...;

should it.

in case problems read in time values: instead of using date type straight read in, utilize long read in time values , (internally) convert them date objects.

java json spring playframework

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -