Invoking a JSON service from OSB

Invoking a JSON service over HTTP from OSB is quite straightforward. Using the Java code that we built in the previous recipe will make working with the JSON messages much simpler.

Getting ready

We'll assume that you have an OSB configuration project in OEPE, and that you have the Jackson and XmlBeans JAR files referred to in the previous recipe, as well as the JAR files produced by that recipe.

How to do it...

  1. We will first create an Oracle Service Bus project. Select the Oracle Service Bus perspective in Eclipse. Right-click on the OSB configuration project, and select New | Oracle Service Bus Project. In the dialog box, enter a name for the project (InvokeJSONCreditCardService) and click on Finish.

    We need to import the web service definition used in the Java project in the previous recipe, as we will implement the DebitCreditCard operation of the CreditCardService.

  2. Right-click on the OSB project, and select Import | Import from the context menu.
  3. In the Import dialog, select General | File System and click on Next.
  4. Within the Package Explorer view, select resources, right-click and select Import. This will open the Import wizard. Select General | File System as the source and click on Next.

    i. Browse to the file directory containing the code samples for this chapter, and within the directory getting ready, select the folder resources.

  5. Within the Import window, ensure resources is checked and the option Create selected folder structure is selected. Click on Finish to import the folder and its contents into the OSB project.
  6. To import the JAR files into the project, right-click on the InvokeJSONCreditCardService OSB project in the Project Explorer, and select New | Folder from the context menu.
  7. Enter the name jars for the folder in the New Folder dialog and click on Finish.
  8. Now, import the following JAR files from the CreditCardServiceMessages project into the jars folder:
    • dist/CreditCardServiceMessages_1.0.jar
    • dist/CreditCardServiceMessagesXmlBeans_1.0.jar
    • lib/jackson-core-asl-1.9.7.jar
    • lib/jackson-mapper-asl-1.9.7.jar

    We now need to inform OSB of the dependencies between the JAR files.

  9. Double-click on the jackson-mapping-asl-1.9.7.jar in the Project Explorer. In the Modify Jar Dependencies dialog, select the jackson-core-asl-1.9.7.jar file from the Available jars pane on the left, and click the Add > button to move it to the Jar references pane. Click on OK.
    How to do it...

    Do the same for the CreditCardServiceMessages_1.0.jar file, but click on the Add All >> button to indicate that it depends on all of the other JAR files in the projecThere will be warnings about classes from the org.joda.time package not being available. This is an optional dependency in the Jackson Mapper, and will not be a problem.

  10. To create and configure a business service, right-click on the OSB project, and select New | Business Service from the context menu. Enter a name for the business service (CreditCardJSON_1.0) and click on Finish.
  11. On the General tab, select Messaging Service as the Service Type.
    How to do it...
  12. On the Messaging tab, select Text as both the Request Message Type and Response Message Type.
    How to do it...
  13. On the Transport tab, specify the Endpoint URI of the target service, and click on the Add button (for the purposes of this example, we've used the URI of a mock service that will help with testing).
    How to do it...

    We'll accept the default values on the HTTP Transport and Message Handling tabs.

  14. Click on the Save icon on the tool bar to save the business service.
  15. To create a proxy service, right-click on the OSB project, and select New | Proxy Service from the context menu. In the Create a new Proxy Service dialog, enter a name for the proxy service (CreditCardService_1.0) and click on Finish.
  16. Next, we will configure the proxy service.

    i. On the General tab of the proxy service definition, click the Browse button to select the binding for the service's interface. Select the CreditCardBinding in the CreditCardService.wsdl, and click OK.

    ii. On the Transport tab, set the Endpoint URI for the service to /ckbk/svc/CreditCard, as specified in the WSDL.

  17. On the Message Flow tab, drag an Operational Branch from the Design Palette and drop it under the CreditCardService_1.0 icon.

    Give the Operational Branch a name (for example, CreditCardServiceOperation) and ensure that the debitCreditCard operation is selected.

  18. Drag a Pipeline Pair into the debitCreditCard flow, and assign it a name (for example, debitCreditCardPipelinePair). Then, drag-and-drop a Stage into the Request Pipeline, and give that a name (for example, debitCreditCardRequestStage). Drag a Java Callout activity (under Message Processing) into the stage, and click on the Browse button in the Properties pane to select the Java method to be invoked.

    In the Select an Archive Resource dialog, select the CreditCardServicesMessages_1.0.jar file, and click on OK.

    How to do it...
  19. In the Select a Java Method dialog, select the debitCreditCardXmlToJson method in the DebitCreditCardConverter class, and click on OK.
    How to do it...
  20. In the Properties pane for the Java Callout activity, set the Expression for the input parameter to $body. Set the Result Value to requestJSON; this is the variable to which the results of the Java method will be assigned.
    How to do it...
  21. Insert a Service Callout activity after the Java Callout.

    i. In the Properties pane for the Service Callout activity, click the Browse button, this will open the Select a Service Resource dialog, select CreditCardJSON_1.0.biz and click OK.

    ii. Enter requestJSON in the Request Variable field of the Properties pane, and responseJSON in the ResponseVariable field, as shown in the following screenshot:

    How to do it...
  22. Drag a Transport Header activity into the Request Action flow of the Service Callout.

    i. In the Transport Headers pane of the Transport Header activity, click on the Add Header button to add a new header to the Outbound Request.

    ii. Set the HTTP header Content-Type to have the value "application/json"; this is the standard MIME type for JSON data.

    How to do it...
  23. Create a Stage in the Response Pipeline, and give it the name debitCreditCardResponseStage.

    i. Drag a Java Callout activity into the stage. Click on the Browse button in the Properties pane to select the Java method to be invoked. In the Select an Archive Resource dialog, select the CreditCardServicesMessages_1.0.jar file and click on OK.

    ii. In the Select a Java Method dialog, select the debitCreditCardResponseJsonToXml method in the DebitCreditCardConverter class and click on OK.

    iii. In the Properties for the Java Callout activity, set the Expression for the input parameter to $responseJSON (the result of the previous Service Callout), and the Result Value to responseXML.

    iv. Add a Replace activity following the Java Callout, and set its properties, as shown in the following screenshot.

    How to do it...
  24. The proxy service and project are now complete. The project can be deployed to your Oracle Service Bus server, and tested using a mock JSON service.

    Note

    For our purposes, we created a mock service using Ruby; this is the file mockservice.rb, which is included with the sample code for this chapter.

    Before running the mock service, you will need a Ruby installation, and to also install the JSON gem.

How it works...

The Java Callout actions are used to convert between the XML and JSON message representations, using the code created in the previous recipe. The Service Callout makes the call through the Business Service to the JSON service, and uses a Transport Header activity to set the HTTP Content-Type header to application/json. The final Replace activity puts the XML response into the body variable, so that OSB will return it to the caller.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.53.119