如何在 Spring Data JPA REST 中读取 post 请求的对象?
我正在使用 Spring Data JPA、REST 以及 H2 数据库。我能够创建数据和在命令行运行程序
的帮助下保存到数据库,我可以使用GET
请求读取数据。
但是我无法使用 POST
请求发布数据。
下面是我的控制器:
@PostMapping(value = {"/create"}, produces = "application/json",consumes = "application/json")
public Customer createCustomer(@RequestBody Customer cust) {
System.out.println("new Customer to be added is :" + cust.toString());
repository.save(cust);
System.out.println("added Successfully");
return cust;
}
有人可以帮助我吗?
客户实体看起来像:
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
protected Customer() {
}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}
public Long getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
尝试传递带或不带 id 的客户对象,但都不起作用。尝试传递
{"firstName":"Michelle","lastName":"Swepson"}
虽然获取我得到的所有内容,但
{"id":6,"firstName":null,"lastName":null} & the passed customer object is
没有被 api 读取..因为我可以看到该对象为
{"id":null,"firstName":null,"lastName":null}
I am using Spring Data JPA
, REST
along with H2
database. I am able to create data & save to database with the help of commandline runner
and i'm able to read the data with the GET
request.
However i am not able to post the data using POST
Request.
Here down is my Controller:
@PostMapping(value = {"/create"}, produces = "application/json",consumes = "application/json")
public Customer createCustomer(@RequestBody Customer cust) {
System.out.println("new Customer to be added is :" + cust.toString());
repository.save(cust);
System.out.println("added Successfully");
return cust;
}
Can anyone help me here?
Customer Entity looks like:
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
protected Customer() {
}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}
public Long getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Tried passing customer object with and without id but none of them worked. tried passing
{"firstName":"Michelle","lastName":"Swepson"}
While fetching all i got is
{"id":6,"firstName":null,"lastName":null} & the passed customer object is
Not getting read by api..as i could see the object as
{"id":null,"firstName":null,"lastName":null}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于 RequestBody 注释。我确实使用了错误的 io.swagger.v3.oas.annotations.parameters.RequestBody 而不是 org.springframework.web.bind.annotation.RequestBody
issue was with RequestBody annotation.i did use the wrong one io.swagger.v3.oas.annotations.parameters.RequestBody instead of org.springframework.web.bind.annotation.RequestBody
问题出在实体上。您应该为属性 id 生成设置器。
发送请求正文时,不要发送 id,因为它是自动生成的。
您还可以将 lombok 依赖项添加到您的项目中,以仅通过注释生成 getter、setter、构造函数等。
The issue is on the entity. You should generate setter for attribute id .
When you send the request body, don't send the id because it is generate automatically.
You can also add the lombok dependency to your project to generate the getter, setter, constructors, etc. with just an annotation.