Reputation: 2776
There is a class annotated with RestResource (Spring 3.1, Jackson 2.3). When we try save object A like that
A: {"prop1":1, "prop2":2}
it saved successfully.
We have following code:
A.setProp2(null);
EntityService.patch(A);
A: {"prop1":1, "prop2":null}
After EntityService.patch(A) execution there is no changes in DB (Oracle 11g), but we want that in DB prop2 will be equals to null too.
Is it a normal behaviour (I think maybe yes, because null-value may understanding like not changed)? Is there a simple way to change this behaviour?
Upvotes: 2
Views: 2379
Reputation: 2776
So, the answer above is right.
Unfortunately, PUT method update all fields and in some cases it maybe a bad solution. For example if we have many nulls in JSON-object, which wasn't modified.
To make Spring PATCH update null values, only when it's change, the best way that I found is extend DomainObjectMerger class. It is a bean of REST MVC config, in which we can override method merge (entity.doWithProperties(new SimplePropertyHandler() {}). In this method we can add addition condition like
sourceValue != targetValue;
independent on null value of sourceValue variable.
Upvotes: 0
Reputation: 28549
When speaking about the HTTP verbs, the PATCH
request describes the differences that should be made to the existing object. It is made so that you can easily create partial updates without exposing a particular resource property throught REST.
In Spring framework, in the context of PATCH
request, the null
value of a property means no change to the property should be made. I guess that spring data rest transpons this logic to the service layer, so in order to actually update your resource with the null
value you should issue a call corresponding to the PUT
semantics
Upvotes: 5