Tarwirdur Turon Tarwirdur Turon - 1 year ago 220
Java Question

Spring data-rest: set null value by PATCH request

I want to set null value to entity by sending null request.

For example:

PATCH: "{deleteDate: null}" to http://localhost/api/entity/1


But it doesn't work.

I found here information how PATCH requests processed:



  • An new instance of Foo is created

  • Foo is populated with all values that have been sent with the request

  • The Foo entity with the id provided by the URI is loaded

  • All properties that differ between the two objects are copied from the new Foo to the persisted Foo, unless the value is null in the new Foo.




Do I understand correctly that it is impossible to set value to NULL with PATCH request to spring-data-rest service API?

Answer Source

In Spring context null values in PATCH method means that there are no changes. If you want write null values you can

1) use PUT method;
2) implement your own DomainObjectMerger class, in which you can extend method merge with condition like

sourceValue != targetValue;

3) use DomainObjectMerger.NullHandlingPolicy configuration.
Depends on your Spring Data REST version.