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,但不幸的是我不知道如何在程序中過濾好數據。
對於標題很簡單,因爲我只發送標題參數。它應該如何查找書籍?
我想你通過一個JSON。您可以以列表形式發送:[{id:「id value」,name:「書名」,isbn:「ISBN code」},{next book}] –