Java Play framework 2.5 Json post请求绑定器
我有一门课,内容如下:Java Play framework 2.5 Json post请求绑定器,java,json,playframework,playframework-2.0,playframework-2.5,Java,Json,Playframework,Playframework 2.0,Playframework 2.5,我有一门课,内容如下: 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
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;
}
我无法绑定窗体的tag属性,我做错了什么
我目前正在使用以下代码解决此问题,但无法使用表单帮助器使用Play framework验证:
NoteForm note = Json.fromJson(json, NoteForm.class);
我能够通过以下方式完成这项工作:
公共列表标签代码>
并且具有如下形式的绑定:
@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")));
}
}
Form<NoteForm> noteForm = formFactory.form(NoteForm.class).bind(request().body().asJson());
奇怪的不列出标签代码>也失败了?只是尝试了一下,但没有成功。你不缺少getter和setter吗?您是否设置了内容类型:application/json header?请看下面的链接@Sivakumar我在发布之前确实查看了这个问题,如果您查看了我发布的代码,我的评论表明我尝试了与您链接的帖子相同的操作//表单noteForm=NOTE_Form.bindFromRequest();
Form<NoteForm> noteForm = formFactory.form(NoteForm.class).bind(request().body().asJson());
Form(of=class models.NoteForm, data={note=adsfadsfdsaaf, id=1, tags[1]=6, userId
=1, tags[0]=5}, value=Optional[models.NoteForm@5cd20029], errors={})