2

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?

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I found, thank you. Some more info - spring.io/understanding/REST
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.