So let's say i have a UserDTO.
@Getter
@Setter
public User
{
private int id;
private String name;
private Integer salary;
private Integer department_id
}
I want to update info about my user partially, via patch request, send id of a person (requiered), and other data, that can be sent partially.
Let's say i have a (2, "Dave", 10, null) user in my db. I'm sending a request, body of request is a simple json:
{"id":2, "salary":20}
And i want to use that info to update the part of the info about the person, not him entirely. The problem is - deserializer puts null's in field that are missing, in other words, he treats my json as if it was:
{"id":2, "salary":20, "name":null, "department_id":null}
and i guess, it is not what i wanna do. A straight mapping could not get the job done, since he treats MISSING fields as EMPTY fields, which are not the same things, like an empty box, and abscence of it. That makes me think that putting body of a request in a UserDTO is not a good decision.
So even a custom mapper from json to DTO won't change a thing. What i want to do is to:
- Extract the user from db by id
- Update all the fields that are present in json
- Save the updated user in db
If it is correct - i would be happy to know how to do such a thing. Otherwise - i am open to any kind of solutions.