在Springboot错误中发布API:列中的空值
我只是想在数据库中创建POST API,但它不起作用并给出错误:
错误:列中“ first_name”关系“客户”中的null值 违反无效约束细节:失败行包含(24、0,, null,null, P>
这是我的API:
@PostMapping("/customers")
public ResponseEntity<Customer> createCustomer(@RequestBody Customer customer) {
try {
Customer _customer = cutomerRepository
.save(new Customer(customer.getSerialNumber(), customer.getFirstName(),customer.getLastName(),customer.getEmail(),customer.getMobileNumber()));
return new ResponseEntity<>(_customer, HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
这是我的客户模型:
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int id;
@Column(name = "serial_number")
private long serialNumber;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
@Column(name = "mobile_number")
private String mobileNumber;
@Column(name = "is_deleted")
private boolean isDeleted;
@OneToMany
private Set <Invoice> invoices;
public Customer(){}
public Customer(long serialNumber , String firstName, String lastName, String email, String mobileNumber) {
this.serialNumber = serialNumber;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.mobileNumber = mobileNumber;
this.isDeleted = false;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getId() {
return this.id;
}
public String getName() {
return this.firstName;
}
public Long getSerialNumber() {
return this.serialNumber;
}
public String getEmail() {
return this.email;
}
public String getMobileNumber() {
return this.mobileNumber;
}
public void setSerialNumber(Long serialNumber) {
this.serialNumber = serialNumber;
}
public void setName(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public boolean isDeleted() {
return isDeleted;
}
public void setDeleted(boolean deleted) {
isDeleted = deleted;
}
public void setEmail(String email) {
this.email = email;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
}
它是JPA存储库,我在Postman中对其进行测试:
Postman Testing
I'm just trying to create post api in database but it's not working and give an error :
ERROR: null value in column "first_name" of relation "customer"
violates not-null constraint Detail: Failing row contains (24, 0,
null, null, [email protected], null, f).
This is my API:
@PostMapping("/customers")
public ResponseEntity<Customer> createCustomer(@RequestBody Customer customer) {
try {
Customer _customer = cutomerRepository
.save(new Customer(customer.getSerialNumber(), customer.getFirstName(),customer.getLastName(),customer.getEmail(),customer.getMobileNumber()));
return new ResponseEntity<>(_customer, HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
This my customer model:
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int id;
@Column(name = "serial_number")
private long serialNumber;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
@Column(name = "mobile_number")
private String mobileNumber;
@Column(name = "is_deleted")
private boolean isDeleted;
@OneToMany
private Set <Invoice> invoices;
public Customer(){}
public Customer(long serialNumber , String firstName, String lastName, String email, String mobileNumber) {
this.serialNumber = serialNumber;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.mobileNumber = mobileNumber;
this.isDeleted = false;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getId() {
return this.id;
}
public String getName() {
return this.firstName;
}
public Long getSerialNumber() {
return this.serialNumber;
}
public String getEmail() {
return this.email;
}
public String getMobileNumber() {
return this.mobileNumber;
}
public void setSerialNumber(Long serialNumber) {
this.serialNumber = serialNumber;
}
public void setName(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public boolean isDeleted() {
return isDeleted;
}
public void setDeleted(boolean deleted) {
isDeleted = deleted;
}
public void setEmail(String email) {
this.email = email;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
}
and it's JPA repository, and I test them in postman :
Postman testing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过打印
@requestbody客户客户的内容?
是因为您的前端发送
first_name
时,您的后端识别firstName
。您可以尝试从Frontend更改JSON以使用Camelcase,也可以在模型中添加@jsonproperty注释
Have you tried printing the content of
@RequestBody Customer customer?
It is because your frontend send
first_name
while your backend recognizesfirstName
. You can try changing your JSON from frontend to use camelCaseOr you can add @JsonProperty annotation in the model