0

I'm trying to develop a CRUD for an application, having the following model:

@Entity
public Person {
  @Id
  @GeneratedValue
  private Long id;

  private String name;
  private String surname;
  private String email;
}

and the following mapping for the update:

@PatchMapping(value = "/update/{id}", consumes = "application/json")
public Person updatePerson(@PathVariable String id, @RequestBody Person payload){
      return personRepository.save(payload);
    }

If I have an object created as:

{
    "name": "John",
    "surname": "Doe"
}

Doing an update request as:

{
    "id": 1,
    "name": "Alfred"
}

will erase all the other fields not defined on the request. Is there a "clever" way to update that I'm failing to notice or I have to check field by field if they are set and update them accordingly?

Onilol
  • 1,315
  • 16
  • 41

0 Answers0