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
CodePudding user response:
Have you tried printing the content of @RequestBody Customer customer?
It is because your frontend send first_name
while your backend recognizes firstName
. You can try changing your JSON from frontend to use camelCase
{
...
firstName: "tets",
...
}
Or you can add @JsonProperty annotation in the model
public class Customer {
...
@JsonProperty("firstName")
@Column(name = "first_name")
private String firstName;
...
}