2017-04-24 18 views
0

我正在使用Spring框架構建rest api,只是爲了學習,並且在使用關係保存數據時遇到了一些問題。 我建立那種書店和My Book實體看起來像這樣 @Entity 公共類圖書{ @Id @GeneratedValue(策略= GenerationType.AUTO) 私人長期ID;使用Postman測試Spring中的保存方法

private String title; 

private String isbn; 

@ManyToOne 
@JoinColumn(name = "author_id") 
@JsonBackReference 
private Author author;`enter code here` 

public long getId() { 
    return id; 
} 

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

public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public String getIsbn() { 
    return isbn; 
} 

public void setIsbn(String isbn) { 
    this.isbn = isbn; 
} 

public Author getAuthor() { 
    return author; 
} 

public void setAuthor(Author author) { 
    this.author = author; 
} 

}

和我的作者實體看起來是這樣的:

@Entity 
@Table(name = "author") 
public class Author { 
public Author(){ 

} 

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

private String name; 

@OneToMany(mappedBy = "author", cascade = CascadeType.ALL) 
private Set<Book> books; 

public long getId() { 
    return id; 
} 

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

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public Set<Book> getBooks() { 
    return books; 
} 

public void setBooks(Set<Book> books) { 
    this.books = books; 
} 
} 

現在我想檢查我的節能方法,工作正常

@RequestMapping(method = RequestMethod.POST, value = "/create") 
public @ResponseBody 
String[] create(@RequestBody Author author) { 
    bookstoreRepository.save(author); 
    return success; 
} 

所以我要檢查這使用例如PostMan,但不幸的是我不知道如何在程序中過濾好數據。

對於標題很簡單,因爲我只發送標題參數。它應該如何查找書籍?

+0

我想你通過一個JSON。您可以以列表形式發送:[{id:「id value」,name:「書名」,isbn:「ISBN code」},{next book}] –

回答

1

由於POST請求主體映射到Author,我們需要發送匹配作者的請求。像下面這樣的東西。會員booksBook類型的集合,所以它應該作爲JSON數組發送。

{ 
    "id": 1234, 
    "name": "TAuthor Name", 
    "books": [ 
    { 
     "title": "book Title", 
     "isbn": "ISBN123" 
    }, 
    { 
     "title": "book Title2", 
     "isbn": "ISBN456" 
    } 
    ] 
} 
相關問題