Play framework JSON list binding

房东的猫 提交于 2020-01-05 10:13:14

问题


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();


来源:https://stackoverflow.com/questions/31828748/play-framework-json-list-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!