Ich versuche, JSON-Daten an einen Spring MVC-Controller zu senden, der mit einem Modell verknüpft ist. Anstatt die JSON-Werte zu erhalten, sind die Werte der Felder des Modells alle NULL.Spring MVC + AngularJs: JSON/Modellwerte auf Null gesetzt
IDE-Debugger:
Chrome:
Ausnahme:
org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!
Controller:
@RequestMapping(value = "/update", method = RequestMethod.POST)
@ResponseBody
public PostResponse update(Setting setting, BindingResult bindingResult) {
return settingService.processUpdate(setting, bindingResult, messageSource);
}
JSON-Daten:
{
"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\"}"
}
Modell:
@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;
}
Aktualisierung Ihrer Daten erzeugt id ' @RequestMapping (value = "/ {id}/update", Methode = RequestMethod.POST) 'und übergeben Sie Ihre ID in' @ PathVariable', so dass Sie Ihre Daten für bestimmte ID –
Die Werte der Felder des Modells aktualisieren können sind alle NULL. Selbst wenn ich die ID hätte, wäre eine Aktualisierung nutzlos. @RequestBody hat den Job gemacht. – arjayads