My JSON data is in request().body().asFormUrlEncoded().get("records")
[{"string":"foo","termId":"793340"},{"string":"bar","termId":"460288"}]
My definition of form:
public static class MyForm { @Constraints.Required public List<Map<String,String>> records; public String someField; }
It does not bind records automatically. Then I tried with POJO:
public static class Record { public String string; public String termId; public void setString(String string) { this.string = string; } public void setTermId(String termId) { this.termId = termId; } }
And adapted the form:
public static class MyForm { @Constraints.Required public List<Record> records; public String someField; }
It also does not automatically bind data. Do I really need to use low level APIs like jackson for this simple use? Any pointer? Could not find copy / paste example, and from jackson I have org.codehaus.jackson and com.fasterxml.jackson in my classpath.
UPDATE 2013-05-10: The secondary someField field has been someField to clarify that records are just one field, not the entire data structure. The answer below (I donโt see the answers on these editing screens, so it doesnโt matter if there is only one) works, but only with entries. Here is an example:
private List<Record> recordsFromRequest() { String[] jsonData = request().body().asFormUrlEncoded().get("records"); Form<Record> recordDummyForm = Form.form(Record.class); Iterator<JsonNode> it = Json.parse(jsonData[0]).iterator(); List<Record> records = new ArrayList<>(); while (it.hasNext()) { records.add(recordDummyForm.bind(it.next()).get()); } return records; }
For other form fields, as usual:
Form<MyForm> form = play.data.Form.form(MyForm.class).bindFromRequest();
So, now I get to all the published form data, and my problem is solved this way (thanks!). However, this is a little ugly. I still can not figure out how to get all the data for publication in one object. If someone answers this, I will update the question and delete this part. Otherwise, I will accept one answer in a couple of days.