2016-04-15 4 views
0

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:

enter image description here

Chrome:

enter image description here

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

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 –

+0

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

Antwort

1

Ich denke, dass die Einstellung Bean überhaupt nicht abgebildet wird.

Sie müssen Spring mitteilen, wie die HTTP-Anfrage den Methodenargumenten zugeordnet werden soll. Wenn Sie Daten zu veröffentlichen, ist der beste Weg, @RequestBody Anmerkung an die jeweilige Methode Argument (setting in Ihrem Fall)

Ändern Sie bitte Ihre Controller-Methode wie folgt hinzuzufügen:

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

Danke, ich habe @RequestBody vergessen :) – arjayads

Verwandte Themen