Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 608
Tarwirdur Turon Avatar asked Jan 09 '15 11:01

Tarwirdur Turon


2 Answers

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.

like image 145
egorlitvinenko Avatar answered Sep 20 '22 14:09

egorlitvinenko


All 3 options from egorlitvinenko's answer will solve the described problem but will have another one:

  • All other properties which were not specified in PATCH request would be "nullified" too.

Seems like spring-data-rest, issue-345 was already fixed in v2.2.x.

like image 42
Dofs Serge Avatar answered Sep 19 '22 14:09

Dofs Serge