使用Postman在Spring中测试保存方法

时间:2017-04-24 13:19:03

标签: java spring postman

我正在使用spring框架构建rest api,只是为了学习而且在保存关系数据时遇到了一些问题。 我正在建造一种书店,我的书实体看起来像这样     @实体     公共课书{     @ID     @GeneratedValue(strategy = GenerationType.AUTO)     私人长身份;

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检查这个,但不幸的是我不知道如何在程序中传递好的数据。

对于标题,它很简单,因为我只发送标题参数。它应该如何寻找书籍?

1 个答案:

答案 0 :(得分:1)

由于POST请求正文映射到Author,我们需要发送与作者匹配的请求。像下面这样的东西。成员books是类型Book的集合,因此它应该作为JSON数组发送。

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