Description Build & Configure Run Troubleshoot Related Topics Start to Run
Directory Location:
D:\Oracle\Middleware\Oracle_Home\wlserver/samples/server/examples/src/examples/javaee8/jsonp/
File Click source files to view code. |
Description |
---|---|
build.xml | Ant build file that contains targets for building and running the example. |
ProcessUserByJsonMergePatch.java | A servlet which handle update user request with JSON Merge Patch. |
ProcessUserByJsonPatch.java | A servlet which handle update user request with JSON Patch. |
User.java | Store the user json string. |
index.html | Index page for this example, which describes this feature and example and verifies the functions. |
web.xml | Web application deployment descriptor. |
This sample illustrates how to use JSON Patch,JSON Merge Patch and JSON Pointer to update a JSON document.
Client send patch request to server like below:
JSON Patch: PATCH /jsonp/ProcessUser HTTP/1.1 Host: localhost:7001 Content-Length: 102 Content-Type: application/json-patch+json Content: [{"op":"replace","path":"/nickName","value":"Tom oracle"},{"op":"replace","path":"/age","value":"33"}]
JSON Merge Patch: PATCH /jsonp/ProcessUser HTTP/1.1 Host: localhost:7001 Content-Length: 35 Content-Type: application/merge-patch+json Content: {"nickName":"Tom merge","age":"34"}
Server side will use JsonPatch
or JsonMergePatch
to update json document like below:
//Json Patch JsonObject oldUser = Json.createReader(new StringReader(userJsonString)).readObject(); // The target to be patched JsonArray patch = Json.createReader(new StringReader(patchJsonString)).readArray(); ; // JSON Patch JsonPatch jsonpatch = Json.createPatch(patch); JsonObject result = jsonpatch.apply(oldUser);
//Json Merge Patch JsonValue oldUser = Json.createReader(new StringReader(userJsonString)).read(); // The target to be patched JsonValue mergePatchJson = Json.createReader(new StringReader(patchJsonString)).read(); // JSON Merge Patch JsonMergePatch mergePatch = Json.createMergePatch(mergePatchJson); JsonValue result = mergePatch.apply(oldUser);
Before working with this example:
This sample is already built and deployed in the wl_server
WebLogic Server Examples domain, so you can
skip these steps and proceed directly to running the example.
D:\Oracle\Middleware\Oracle_Home\wlserver/samples/server/examples/src/examples/javaee8/jsonp
directory.
ant build
ant deploy
wl_server
WebLogic Server Examples
domain.ant run
Input the nick name and age, then click the save button. You can see the following message on the console.
May 23, 2018 1:46:54 PM examples.jsonp.jsonp11.ProcessUserByJsonPatch doPost INFO: Before update: {"UserName":"Tiger","email":"example@oracle.com","age":"33","nickName":"tom oracle"} May 23, 2018 1:46:54 PM examples.jsonp.jsonp11.ProcessUserByJsonPatch doPost INFO: Request json [{"op":"replace","path":"/nickName","value":"tom oracle"},{"op":"replace","path":"/age","value":"33"}] May 23, 2018 1:46:54 PM examples.jsonp.jsonp11.ProcessUserByJsonPatch doPost INFO: Content-Type application/json-patch+json Update "/nickName" --> "tom oracle" May 23, 2018 1:46:54 PM examples.jsonp.jsonp11.ProcessUserByJsonPatch doPost INFO: Invoke updateUserByPatch method May 23, 2018 1:46:54 PM examples.jsonp.jsonp11.ProcessUserByJsonPatch doPost INFO: After update: {"UserName":"Tiger","email":"example@oracle.com","age":"33","nickName":"tom oracle"} May 23, 2018 1:55:10 PM examples.jsonp.jsonp11.ProcessUserByJsonMergePatch doPost INFO: Before update: {"UserName":"Tiger","email":"example@oracle.com","age":"33","nickName":"tom oracle"} May 23, 2018 1:55:10 PM examples.jsonp.jsonp11.ProcessUserByJsonMergePatch doPost INFO: Request json {"nickName":"tom merge","age":"34"} May 23, 2018 1:55:10 PM examples.jsonp.jsonp11.ProcessUserByJsonMergePatch doPost INFO: Content-Type application/merge-patch+json May 23, 2018 1:55:10 PM examples.jsonp.jsonp11.ProcessUserByJsonMergePatch doPost INFO: Invoke updateUserByMergePatch method May 23, 2018 1:55:10 PM examples.jsonp.jsonp11.ProcessUserByJsonMergePatch doPost INFO: After update: {"UserName":"Tiger","email":"example@oracle.com","age":"34","nickName":"tom merge"}
Copyright © 2020, Oracle and/or its affiliates. All rights reserved.