2016-04-15 81 views
0

我想提交json數據到映射模型的Spring MVC控制器。與獲取json值不同,模型的字段值全部爲NULL。Spring MVC + AngularJs:JSON /模型值設置爲空

IDE調試器:

enter image description here

鉻:

enter image description here

例外:

org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null! 

控制器:

@RequestMapping(value = "/update", method = RequestMethod.POST) 
    @ResponseBody 
    public PostResponse update(Setting setting, BindingResult bindingResult) { 
     return settingService.processUpdate(setting, bindingResult, messageSource); 
    } 

JSON數據:

{ 
    "updatedAt":1460600207000, 
    "id":1, 
    "createdBy":null, 
    "description":"This is a setting", 
    "code":"MY_SETTING", 
    "value":"{\"id\":\"1018\",\"title\":\"Another setting\",\"code\":\"220-203-10-101\"}" 
} 

型號:

@Entity 
@JsonIgnoreProperties(ignoreUnknown = true) 
public class Setting { 
    @Id 
    @GeneratedValue 
    @Column 
    private Integer id; 

    @Column(unique = true) 
    private String code; 

    @Column 
    private String description; 

    @Column 
    private String value; 

    @Temporal(TemporalType.TIMESTAMP) 
    @Column(nullable = false) 
    private Date createdAt; 

    @Temporal(TemporalType.TIMESTAMP) 
    @Column(nullable = false) 
    private Date updatedAt; 

    @NotFound(action = NotFoundAction.IGNORE) 
    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name="FK_createdByUserId") 
    private User createdBy; 

    public Setting() {} 

    public Setting(String code, String description, String value, Date createdAt, Date updatedAt, User createdBy) { 
     this.code = code; 
     this.description = description; 
     this.value = value; 
     this.createdAt = createdAt; 
     this.updatedAt = updatedAt; 
     this.createdBy = createdBy; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getCode() { 
     return code; 
    } 

    public void setCode(String code) { 
     this.code = code; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public String getValue() { 
     return value; 
    } 

    public void setValue(String value) { 
     this.value = value; 
    } 

    public Date getCreatedAt() { 
     return createdAt; 
    } 

    public void setCreatedAt(Date createdAt) { 
     this.createdAt = createdAt; 
    } 

    public Date getUpdatedAt() { 
     return updatedAt; 
    } 

    public void setUpdatedAt(Date updatedAt) { 
     this.updatedAt = updatedAt; 
    } 

    public User getCreatedBy() { 
     return createdBy; 
    } 

    public void setCreatedBy(User createdBy) { 
     this.createdBy = createdBy; 
    } 
+0

更新你對生成的ID數據' @RequestMapping(value =「/ {id}/update」,method = RequestMethod.POST)'並在'@ PathVariable'中傳遞你的id,這樣你就可以更新數據的特定ID的數據 –

+0

模型的字段值都是NULL。即使我有ID,更新也沒用。 @RequestBody完成了這項工作。 – arjayads

回答

1

我猜測,設置豆是不是在所有映射。

您需要告訴spring如何將http請求映射到方法參數。如果您發佈的數據,最好的辦法是@RequestBody註解(你的情況setting)添加到相關方法參數

修改你的控制器的方法是這樣的:

@RequestMapping(value = "/update", method = RequestMethod.POST) 
    @ResponseBody 
    public PostResponse update(@RequestBody Setting setting, BindingResult bindingResult) { 
     return settingService.processUpdate(setting, bindingResult, messageSource); 
    } 
+0

謝謝,我忘了@RequestBody :) – arjayads