【发布时间】:2017-09-21 04:50:04
【问题描述】:
我正在使用 spring 框架构建 rest api,只是为了学习,在保存关系数据时遇到了一些问题。 我正在建立一种书店,我的书实体看起来像这样 @实体 公共课本{ @ID @GeneratedValue(策略 = GenerationType.AUTO) 私有长ID;
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:"name of the book", isbn:"ISBN code"}, {next book}]