Play framework JSON list binding

2019-08-12 10:15发布

问题:

I am new into Play, and I want to try to bind a List of String from a JSON post.

I do the following:

import play.data.validation.Constraints;
import java.util.ArrayList;
import java.util.List;

Form<Person> PERSON = new Form<>(Person.class);
Form<Person> filledForm = PERSON.bind(request().body().asJson());

Person class {

  @Constraints.Required
  @Constraints.Email
  private String email;

  @Constraints.Required
  private List<String> adresses = new ArrayList<>();

}

I get the following message:

"matches":[
"This field is required"
]

回答1:

This line causes problems:

Form<Person> filledForm = PERSON.bind(request().body().asJson());

Replace it with:

Form<Person> filledForm = PERSON.bindFromRequest();