2

我有以下類:播放框架2.5的Json POST請求粘結劑

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我張貼的JSON從$阿賈克斯看起來像下面這樣的對象:

{ 
     "id":"1", 
     "userId":"1", 
     "note":"adsfadsfdsaaf", 
     "tags":["5","6"] 
    } 

我無法獲取表單的標籤屬性來綁定,我做錯了什麼?

我目前正在解決這個問題,只要使用下面的代碼,但我不能使用表單助手利用遊戲架構驗證:

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

奇怪。 '列出標籤;'也失敗? – jsonmurphy

+0

剛剛嘗試過,並沒有奏效。 – develop4fun2011

+0

不要錯過getters和setter?你是否設置了Content-type:application/json頭文件? – Tijkijiki

回答

1

我是能夠使採用這種工作方式:

public List<Integer> tags;

和具有該形式像這樣結合:

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

結合後登錄的noteForm給我這個(使用輸入):

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