I'm implementing a PATCH
request method in my RESTful service (Spring, Java). I'm consuming DTO objects in my controller methods. I'm using Dozer to map those DTO's to entities.
With the PATCH
request method I have an obvious problem: a field with the null value could mean either that the entity field should be updated to null, or that the entity field should be left unchanged. Having looked through some posts in the internet, I decided to accept in uri parameters a list of fields which are to be nulled explicitly (I'll refere to this list as nullFields
). All the other fields would stay unchanged if the received value is null.
Now I can see 3 possible ways to implement this solution:
- To have Dozer map objects as usual, skipping fields with null values. Then explicitly set the fields from
nullFields
to null. I would have to iterate over the values ofnullFields
and invoke appropriate setter methods of the entity. I don't like this option because I would have to change this field-to-setter mapping every time I add, delete or rename object fields. - Same as above - Dozer maps my objects, then set some of the fields to null explicitly - but using reflection. Then I wouldn't have to maintain that field-to-setter mapping described in 1. But I would have to use reflection which is considered to be a bad practice.
- Get Dozer to skip fields included in
nullFields
by changing mapping configurations dynamically or in some other way. But I wasn't able to find out how to do it. So, my question is, is it possible?
I would also appreciate any other ways to implement PATCH
request method. I've considered consuming JSON objects instead of DTO's, but I don't quite like this alternative unless I have no other choice.