Description   Build & Configure   Run   Troubleshoot   Related Topics  Start to Run

About the Example


Files Used in the Example

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.

Description

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);

Building and Configuring the Example

Prerequisites

Before working with this example:

  1. Install WebLogic Server, including the examples.
  2. Start the Examples server.
  3. Set up your environment.

Build and Deploy the 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.

  1. Change to the D:\Oracle\Middleware\Oracle_Home\wlserver/samples/server/examples/src/examples/javaee8/jsonp directory.
  2. Execute the following command:

    ant build

    to compile and stage the example.
  3. Execute the following command:

    ant deploy

    to deploy the example in the wl_server WebLogic Server Examples domain.


Running the Examples

  1. Complete the steps in the "Building and Configuring the Example" section.
  2. Execute the following command:

    ant run

    which opens your default browser to display the json-p 1.1 new features sample application home page.
  3. 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"}
    

Troubleshooting


Related Topics


Copyright © 2020, Oracle and/or its affiliates. All rights reserved.