I have a Hibernate model with id, name and surname. I am using it to get data from the database and then at the GET end-point is this one:
@GetMapping(value = "/contacts", produces = MediaType.APPLICATION_JSON_VALUE)
public List<Contact> allContacts() {
return contactService.findAll();
}
As you can see it returns the Contact
object. Actually it is a Hibernate entity.
The problem is that when I use this code
@PostMapping("/contacts")
public Contact createContact(Contact contact) {
return contactService.createContact(contact);
}
it asks not only name
and surname
but also the id
. POST methods should not ask for id
since they are not created yet. What should I do so that it doesn't ask for an id
?
Edit: Here is the Contact.java class
import lombok.Data;
import javax.persistence.*;
@Entity
@Data
public class Contact {
public Contact() {
}
public Contact(Integer id, String name, String surname) {
this.id = id;
this.name = name;
this.surname = surname;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(columnDefinition = "serial")
private Integer id;
private String name;
private String surname;
}