/springboot-patch-operations

Demo of how JSON Patch can be applied to HTTP PATCH operations written as Springboot service

Primary LanguageJava

Getting Started

This is an example SpringBoot project which demonstrates the IETF JSON Patch standard as further discussed and evaluated in this article.

How to Run

  • Open the project in IntelliJ Idea or Eclipse.
  • Run the class SbHttpPatchDemoApplication.java file in io.github.isuru89.sbpatch package.
  • Server should be running in 8080 port.

Example Curls

  • Creating a user with a single email
curl --location --request POST 'http://localhost:8080/users' \
--header 'Content-Type: application/json' \
--data-raw '{
   "userName": "jondoe",
   "firstName": "Jon",
   "lastName": "Doe",
   "primaryEmail": "jondoe@gmail.com",
   "secondaryEmails": [
       {
           "email": "email1@gmail.com"
       }
   ]
}'
  • Reading back the created user. (Replace the id with the id in returned response of the created user)
curl --location --request GET 'http://localhost:8080/users/{id}'
  • Patch user's firstName (Replace the id with your created user id in the url)
curl --location --request PATCH 'http://localhost:8080/users/{id}' \
--header 'Content-Type: application/json' \
--data-raw '[
    { "op": "replace", "path": "/firstName", "value": "Jon Updated" }
]'
  • Add a new email address to the end of email list and make it primary
curl --location --request PATCH 'http://localhost:8080/users/{id}' \
--header 'Content-Type: application/json' \
--data-raw '[
    { "op": "add", "path": "/secondaryEmails/-", "value": { "email": "email2@yahoo.com" }},
    { "op": "add", "path": "/primaryEmail", "value": "email2@yahoo.com" }
]'
  • Remove the first email address in the list
curl --location --request PATCH 'http://localhost:8080/users/{id}' \
--header 'Content-Type: application/json' \
--data-raw '[
    { "op": "remove", "path": "/secondaryEmails/0" }
]'

Try changing patch body according to the standard and see how it works.

Enjoy!