gpt4 book ai didi

java - Play framework 2.5 Json post request binder

转载 作者:行者123 更新时间:2023-11-30 10:46:02 25 4
gpt4 key购买 nike

我有一个包含以下内容的类(class):

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;
}

还有这样的 Controller 操作:

@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"]
}

我无法绑定(bind)表单的标记属性,我做错了什么?

我目前正在通过使用以下代码来解决这个问题,但我无法使用表单助手来使用 Play 框架验证:

NoteForm note = Json.fromJson(json, NoteForm.class);

最佳答案

我能够使用以下方法完成这项工作:

public List<Integer> tags;

并像这样绑定(bind)表单:

Form<NoteForm> noteForm = formFactory.form(NoteForm.class).bind(request().body().asJson());

记录 noteForm绑定(bind)后给我这个(使用您的输入):

Form(of=class models.NoteForm, data={note=adsfadsfdsaaf, id=1, tags[1]=6, userId
=1, tags[0]=5}, value=Optional[models.NoteForm@5cd20029], errors={})

关于java - Play framework 2.5 Json post request binder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36769337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com