Spring REST sample with json merge patch support
There are two RFCs on the JSON patch to support
- JSON Merge Patch - https://tools.ietf.org/html/rfc7386
- JavaScript Object Notation (JSON) Patch - https://tools.ietf.org/html/rfc6902
Spring REST is yet to inherently support these RFCs. Spring Data REST supports both these RFCs.
Following steps will add patch support in the REST controller
- Consume application/merge-patch+json media type
- Retrieve the request body as Json String
- Parse the input Json String to JsonNode
- Construct JsonMergePatch object from the above parsed JsonNode
- Convert the existing entity object to JsonNode
- Apply the JsonMergePatch on existing entity object
- Convert the patched JsonNode back to entity object
- Save the entity object to the repository
- JSON Merge patch has limitations around merging array elements and setting null values
References
- FGE JSON Patch library - https://github.com/fge/json-patch
- Spring DATAREST commit - https://github.com/spring-projects/spring-data-rest/commit/ef3720be11f117bb691edbbf63e38ff72e0eb3dd
- http://erosb.github.io/post/json-patch-vs-merge-patch/