Soa 12.2.1 New Feature – End to End JSON and Javascript Example

SOA 12.2.1 provides support for end-to-end JSON and Javascript. We don’t have do internal mapping between XML and JSON as in 12.1.3.

As part of this post, let us implement a simple REST service to demonstrate this new feature – End to End JSON with Javascript.

Let us create a very simple SOA Composite which exposes a JSON based REST interface, manipulates the values using Javascript and calls an external JSON based service.

 

End to End JSON with Javascript
Rest Service

External JSON Service Setup

For external JSON based REST service let us use a  dummy JSON Server.

Start Json Server
Start Json Server

Content of db.json file.

{
 "posts": [
 {
 "id": 1,
 "title": "SOA Composite Sensor",
 "author": "Krishna Hanumantharao"
 },
 {
 "title": "OSB Result Cache",
 "author": "Girish Patel",
 "id": 2
 }
 ],
 "comments": [
 {
 "id": 1,
 "body": "some comment",
 "postId": 1
 }
 ],
 "profile": {
 "name": "Krishna"
 }
 }

Result of get query from the REST service

Output from REST GET Query
Output from REST GET Query

Create Reference

With the dummy REST service setup, let us create SOA Application and create a reference to external REST service. Drag and drop a “REST” adapter to reference section.

Create External Reference
Create External Reference

Configure Resources

Create Resource
Create Resource

Create Method Reference with highlighted values for GET. Specify parameter name as “id”.

Create Method Reference for Get
Create Method Reference for Get

Create Method Reference with highlighted values for POST.

Create Method for Post
Create Method Reference for Post

Screen with configured details. Click “Finish” to create the reference.

Reference Configuration
Resources Configuration

Create Service Entry

Drag and drop a “REST” adapter to service section. Configure to have following values.

While doing  GET method configuration, create  a “parameter” with name “id”.

Service Entry Configuration
Service Entry Configuration

Create BPEL Component

Drag and drop a BPEL component and configure with highlighted value.

Create BPEL Component
Create BPEL Component

click on plus sign to add REST methods. Add both the methods available in service entry wadl.

BPEL Interface Get metho
BPEL Interface getPost  method
BPEL Interface REST Methods
BPEL Interface Methods

Wire the BPEL component to External reference. As we have two methods, created BPEL component will have pick activity.

BPEL Pick Activity
Created BPEL

Create five variables.

Create Variables
Create Variables

Click on OnMessage in “getPost” branch. Copy the user passed value, available in “rest.query.id” to “id” variable.

rest.query.id -> variable defined during creation of REST service entry interface.

Capture Input Value
Capture Input Value

Add an Invoke activity in each branch. Configure Invoke for “getPost” interface

Configure Get Input
Configure Get Input

As part of configuration, pass the value passed by user, stored in “id” var  to “rest.query.id” variable.

Set value for REST Query Param
Set value for REST Query Param

rest.query.id ->variable defined during creation of external REST service reference

Add a assign activity after the invoke. Output of external REST service will be available in GET_OP variable. Copy the value to Output variable of our REST Service.

Assign Output
Assign Output

This completes the “GET” branch.

 

Similarly configure Invoke for “doPost” interface

Configure Post Input
Configure Post Input

For “POST”, let us define the input of our REST service to be the following. Here name is split into firstname and lastname. We will concatenate and pass it as single name to the external REST Service.

{
id: 3,
firstname: "Ken",
lastname: "Ben",
title: "Introduction to Coherence"
}

Add an assign activity below receive . Add copy rules to copy

id -> id

concat(firstname, lastname) -> author

title ->title

Set Variables for POST - REST Query
Set Variables for POST

That completes POST branch.

Now, let us deploy and test it.

Test GET interface

Give the value of “1” or “2”. We should get details of record for that id in JSON format.

REST Get Results
REST Get Results

Test POST interface

Let us give following value. POST will add a new record .

{
"id": 3,
"firstname": "Ken",
"lastname": "Ben",
"title": "Introduction to Coherence"
}

After testing with the above input ,verify the result by invoking “GET” to see the newly added record

End to End JSON with Javascript
Newly Added Record

This completes the creation and testing  of a very simple SOA Composite which exposes a JSON based REST interface, manipulates the values using Javascript and calls an external JSON based service.

Please refer “Using Javascript and JSON in BPEL Component” for more information on using Javascript.

 

 

8 thoughts on “Soa 12.2.1 New Feature – End to End JSON and Javascript Example

  1. hey can you tell where are you defining the input for the POST that json you mentioned, where exactly in the composite we have to put it?

    1. Do you mean the following POST input?.

      {
      id: 3,
      firstname: “Ken”,
      lastname: “Ben”,
      title: “Introduction to Coherence”
      }

      It doesn’t need to be defined in the composite. As we have defined the input and output parameters to be JSON object, we can pass any valid JSON object. The above JSON needs to be used while testing.

      1. but when you are making assignments, it has to be somewhere in receive and response from where i can make the mappings.

        like you are making assignments doPost_InputVar.id with Post_IP.id in the picture above how can i have the elements pf id, full name etc without specifying the input json.

        1. I am not sure whether you are asking where specifically, “id”, “first name”, etc are defined.
          Unlike XML, we are not using a JSON schema which defines all the elements. When you create a service entity, you will see variables of type JSON object are created.

          In this case , for POST, a variable called RestSvc_doPost_InputVar , of type JSON Object is created. In this blog, we are making an assumption that Input JSON will have fields like “id”, “firstname”, “lastname” etc and we accordingly access those inside bpel as process.RestSvc_doPost_Inputvar.id , process.RestSvc_doPost_Inputvar.fullname,etc.

          The external rest service, has fields like id, author and title as part of the JSON object. There is no specific definition for id, author, title inside soa composite. We set values to those using the Post_IP.id, Post_IP.author, etc.

          Unfortunately , I dont have the sample project. Try creating the project and if you get an error , send me the project.

          1. hi thanks for the reply,
            I am not getting an error, but my issue is i am creating a POST request and i want to specify few child elements for it using JSON, so how can i specify them, like you specified id fname, lname etc.

          2. You can define your own JSON object with field you like. For example, this is another JSON with different fields.

            {
            BookId: 3,
            AuthorName: “Ken”,
            AuthorLastName: “Ben”,
            BookTitle: “Introduction to Coherence”,
            AnotherField: “someValue”,
            YetAnotherField: “somevalue”

            }
            While Testing, assuming the above will be passed as input to your service, you can access the passed JSON object in assign activity like
            process.RestSvc_doPost_Inputvar.BookId
            process.RestSvc_doPost_Inputvar.AuthorName
            process.RestSvc_doPost_Inputvar.AnotherField
            process.RestSvc_doPost_Inputvar.YetAnotherField

            Hope this helps.

  2. I have noticed you don’t monetize your website, don’t waste your traffic, you can earn additional cash every month because you’ve got hi quality content.
    If you want to know how to make extra money, search for:
    Boorfe’s tips best adsense alternative

Leave a Reply

Your email address will not be published. Required fields are marked *