【问题标题】:Testing the saving method in Spring using Postman使用 Postman 测试 Spring 中的保存方法
【发布时间】: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}]

标签: java spring postman


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2019-08-04
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 2018-09-05
    • 2015-01-04
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    相关资源
    最近更新 更多