'Apply JSON Patch to a collection

I have a Spring Boot application in which I want to enable the JSON Patching of multiple resources at the same time, as described in this question. My endpoint accepts a javax.json.JsonPatch as parameter and returns 204 NO CONTENT upon success.

Now, my collection patching strategy is:

  1. Obtain the id of each entity that needs to be updated, which can be found in the path component of the JsonPatch object.
  2. Fetch all the entities with the given ids.
  3. Convert each entity to its JSON representation.
  4. Apply the corresponding patch.
  5. Persist the updated entities.

My question is how can I obtain all the entity ids from the path attribute of the objects, if that is possible without doing regex parsing or other such string manipulating operations?

If this is not the best approach at patching several documents in the same operation, could you provide a better strategy?

Thanks!



Solution 1:[1]

Why you need to put the ids inside the jsonPatch? I suggest to accept a Map<String,JsonPatch> where String key is your entity Id and JsonPatch value the list of the operations to perform on that entity.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael Cauduro