1

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:

  1. 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 of nullFields 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.
  2. 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.
  3. 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.

Andrew
  • 21
  • 5

1 Answers1

0

I had similar problem with PATCH requests, to handle null values I have written custom dozer mapper. You can read this for information bypass mapping of null or empty values

The other way to implement PATCH request is given in (JSON) Patch. Also, there is open source implementation available for this RFC Java Library to find / apply JSON Patches.

Community
  • 1
  • 1
Ravi
  • 124
  • 3
  • 12