2014-06-20 45 views
0

我有POST請求控制器:春季啓動POST方法JSON數據驗證

@RequestMapping(value="/projects/add", method=RequestMethod.POST) 
@ResponseBody 
public String addProject(@Valid @RequestBody usersProjects project, BindingResult result) { 
    if(result.hasErrors()) { 
     System.out.println("Error ::: " +result.toString()); 
    } 

    logger.info(project.toString()); 

    usersProjects.addProject(project); 

    return "ok"; 
} 

實體數據庫表:

@Entity 
@Table(name="users_projects") 
public class UsersProjects { 

    @GeneratedValue(strategy=GenerationType.AUTO) 
    @Id 
    private Long id; 

    @NotEmpty 
    private String name; 

    private Boolean approved; 

    @NotEmpty 
    @ElementCollection 
    private List<String> spaces; 
    @NotEmpty 
    @ElementCollection 
    private List<String> owners; 

    @OneToMany(cascade=CascadeType.ALL, 
      fetch=FetchType.EAGER) 
    private Set<UsersProjectJobs> jobs; 

    @OneToMany(cascade=CascadeType.ALL, 
      fetch=FetchType.EAGER) 
    private Set<UsersProjectData> data; 


    public UsersProjects() { 

    } 

    public UsersProjects(String name, List<String> spaces, List<String> owners) { 
     this.name = name; 
     this.spaces = spaces; 
     this.owners = owners; 
     this.approved = false; 
    } 

    public Boolean getApproved() { 
     return approved; 
    } 
    public void setApproved(Boolean approved) { 
     this.approved = approved; 
    } 
} 

當我嘗試後,捲曲數據到服務器:

curl -i -H "Content-Type:application/json" -H "Accept:application/json" -X POST -d '{ "name": "123", "spaces": ["123"], "owners": ["123"]}' http://localhost:8080/projects/add 

還有錯誤:

客戶端發送的請求在語法上不正確。

我發現很多主題描述了當Spring無法驗證接收到的JSON數據時發生此錯誤。什麼我應該添加到log4j.properties更詳細的輸出或可能有人知道鏈接到關於模型驗證的好主題?

謝謝。

UPD log4j的配置:

# Root logger option 
log4j.rootLogger=DEBUG, stdout 

log4j.logger.org.springframework=DEBUG, stdout 
log4j.logger.org.springframework.beans=DEBUG, stdout 
log4j.logger.org.springframework.context=DEBUG, stdout 
log4j.logger.org.springframework.http=DEBUG, stdout 
log4j.logger.org.springframework.web=DEBUG, stdout 
log4j.logger.org.hibernate=DEBUG, stdout 
log4j.category.org.springframework.beans.factory=DEBUG, stdout 

log4j.logger.org.apache.catalina.core=DEBUG 
log4j.logger.org.apache.catalina.session=DEBUG 

log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.Target=System.out 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 

UPD UPD

public class validatorAddProject { 
    private String name; 
    private List<String> spaces; 
    private List<String> owners; 

    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public List<String> getSpaces() { 
     return spaces; 
    } 
    public void setSpaces(List<String> spaces) { 
     this.spaces = spaces; 
    } 
    public List<String> getOwners() { 
     return owners; 
    } 
    public void setOwners(List<String> owners) { 
     this.owners = owners; 
    } 
} 


@RequestMapping(value="/projects/add", method=RequestMethod.POST) 
@ResponseBody 
public String addProject(@Valid @RequestBody validatorAddProject form, BindingResult result) { 
    if(result.hasErrors()) { 
     System.out.println("Error ::: " +result.toString()); 
    } 

    usersProjects project = new usersProjects(form.getName(), form.getSpaces(), form.getOwners()); 
    usersProjects.addProject(project); 

    return "ok"; 
} 
+0

是完整的'用戶項目'類嗎?這是處理程序方法中的類型嗎?將日誌轉到DEBUG並再次查看。 –

+0

我的log4j.properties文件:http://pastebin.com/6KuuYT61我沒有在這裏看到有關驗證錯誤的任何記錄。是的,這是完整的實體類。 –

+0

你的日誌應該包含你爲什麼得到400的信息。檢查它們。如果他們不這樣做,您的記錄器配置錯誤。 –

回答

0

我想你需要補充制定者或@JsonCreator裝點你的構造。同樣升級到最新的Boot版本,如果你正在使用(我從標籤猜測),應該在響應中公開BindingException,如果是這樣的話。

+0

謝謝。我已經將JsonCreator和JsonProperty添加到實體類中,它可以工作,但不像我想要的那樣。我創建了另一個公開課,只有我想要添加到數據庫的字段。 –