2016-06-25 35 views
1

客戶端和服務器之間的連接正常工作,並且在服務器上調用了正確的函數addEpic。問題是,只是在服務器上創建了一個新的Epic實例,但不使用來自客戶端的屬性。 @RequestBody似乎是問題所在。不應該自動從JSON數據轉換@RequestBody到指定的類? Epic是@Entity課程的根本問題?使用@RequestBody將Angular2中的對象發佈到Spring RestContoller中

也可能是身體在客戶端產生錯誤。 的console.log(體)顯示:

{"epic":{"id":"f97d885a-410f-fa6d-7adc-291e63d35341", "name":"OurName"}} 

但我招搖的UI顯示人體模型瑪:

{"id":"string", "name":"string"} 

客戶

addEpic(epic:Epic):Observable<any> { 
    let body = JSON.stringify({epic}); 
    let headers = new Headers({'Content-Type': 'application/json'}); 
    let options = new RequestOptions({headers: headers}); 

    return this.http.post(url + "addEpic", body, options) 
     .map(this.extractHeader) 
     .catch(this.handleError); 
    } 

export class Epic { 
    id: string; 
    name: string; 
} 

服務器

@RequestMapping(value="/addEpic", method = RequestMethod.POST) 
    public ResponseEntity<Void> addEpic(@RequestBody Epic epic) { 

     // Here it seems that constructor of epic is called and a new instance is created 
     epicRepository.saveAndFlush(epic); 

     return new ResponseEntity<Void>(HttpStatus.CREATED); 
    } 

@Getter 
@Setter 
@NoArgsConstructor 
@AllArgsConstructor 
@Entity 
public class Epic implements Serializable { 
    private static final long serialVersionUID = -670305953055479441L; 

    @Column 
    private String id; 

    @Column 
    private String name 
} 

回答

3

你的實體Epic有兩個屬性'id'和'name'。 在JSON中:

{"id":"string", "name":"string"} 

這正是Swagger告訴你的。

所以你的客戶是做錯了,就應該有創建JSON像

let body = JSON.stringify(epic); 

只需卸下superflous {}圍繞「史詩」。

相關問題