我有以下课程:
public class NoteForm {
public Integer id;
@Constraints.Required
public Integer userId;
@Constraints.Required
public String note;
// cant get any of the following to work
public Int[] tags;
public String[] tags;
public List<int> tags;
}
像这样的控制器动作:
@BodyParser.Of(BodyParser.Json.class)
public Result updateNote(){
Form<NoteForm> noteForm = NOTE_FORM.bind(request().body().asJson());
//also have tried the following
//Form<NoteForm> noteForm = NOTE_FORM.bindFromRequest();
if(noteForm.hasErrors()){
return badRequest(noteForm.errorsAsJson());
}else{
noteService.saveNote(noteForm.get());
return jsonResult(ok(Json.toJson("Save Succeeded")));
}
}
当我使用如下所示的 json 对象从 $.ajax 发布 JSON 时:
{
"id":"1",
"userId":"1",
"note":"adsfadsfdsaaf",
"tags":["5","6"]
}
我无法绑定表单的标签属性,我做错了什么?
我目前正在通过仅使用以下代码来解决此问题,但我无法使用表单帮助程序来利用 Play 框架验证:
NoteForm note = Json.fromJson(json, NoteForm.class);