Using External REST Services with JDeveloper 18.104.22.168 (Part 1)
In this blog we look how we can use an external REST service with JDev 22.214.171.124. To make things more interesting we don’t use an ADF based REST service and we look how to get nested data into the UI.
For this sample we like to create an application which allows to search for music tracks and show the results in a table or listview. To get the music data we use a REST service and to display the data we use ADF faces application.
In part 1 we create the application and the project for the REST Data Control. Part 2 we will create the UI using the REST Data Control.
Setting up an external REST services
Let’s start be selecting a REST service which is available for public use without the need to get a key first. We use such a service to make it easier for you to run the sample and to look at the code. If we would need a key to use the API, you would need to register yourself with the service before you can run the sample.
There are a couple of such REST services like Spotify, iTunes or MusicBrainz which offer search APIs for music data as public REST service. Spotify we have to eliminate from the list as this service requires an API key since Mai 2017, meaning that it’s not public available without you register yourself before using it. ITunes REST API allows public access and the data structure returned is very simple. The result for a search get you everything in a flat structure. This will make things easy, too easy 🙂
For this sample where we like to show how to work with more complex data structures returned by a REST service. So, the final vote for this blog goes to MusicBrainz (MusicBrainz Rest API).
Musicbrainz REST API comes in different versions (V1 and V2). The current version v2 is what we are interested in as V1 is already deprecated. The documentation tell us, that the service is an XML style REST service. However, there is a JSON style REST service available too. This JSON style RSET service is what we use for the sample.
Before we implement the REST service calls we need to find out how to search for the data we like to show. For this a tool like Postman is a great help. Postman allows you to enter calls to REST services in a browser like UI. You can set all kind of headers, e.g. below we see a sample of the Postman UI (in the result a couple of sub structures are folded to show the relevant data). The query searched for recordings named ‘yesterday’ and asked for a result in JSON format:
Before we begin implementing something which uses the external REST service we have to think about the use case. We like to implement a music title search using the external MusicBrainz REST service. A user should be able to enter a music title or part of a music title and as a result of the search she/he should get a list of titles, the artist or artists, the album and an id.
Creating a REST Web Service Project
After we looked at the REST Service and the data it returned we have identified the data we need to get from the REST service. The first step is to create a project which communicate with the REST service.
We create a normal Fusion Web Application which will create a ADF model project and a view controller project. If you need a script on how to do this you can look at Writing Reproducible Test Cases: Why and How. The model project we don’t need for this sample. You can delete it or just leave it empty.
For the access to the MusicBrainz data we create a new REST Web Service Project inside the application:
Name it and go through the rest of the wizard
Before we create the web service data control, we need to create a REST Connection from the resource pallete we create a new IDE connection of REST type
Then we later need data returned from the REST service which the wizard uses to produce the data structure. A simple way to get such data is to use e.g. Postman to call the REST service:
Copy the result (all of it!) and save it to a file. Next we create a new Web Service Data Control from the gallery
Select the ‘WebService Data Control (SOAP/REST)’
And fill out the wizard. Select the REST connection created before
In the next image click on the green ‘+’ sign
And change the path to ‘/ws/2/recording’, select JSON as data format and checkmark the GET method to enter ‘recording’ into the field.
In the next screen we need to select ‘Parse from Sample Code’ and copy the content of the file we saved from Postman into the textarea
And finally test the Web Service Data Control
The finish the wizard. Now we can test the data contron by finding the DataControl.dcx file in the project and right click on it. Choose ‘Run’ from the context menu:
In the dialog window right click hte data control and choose ‘Operations’
Fill in the fields and click the execute button
The result should look like
You can copy the return value into an editor to fully see it. If you don’t get the successful result, check the steps against the ones in the blog.
This concludes part 1 of this series. In part 2 we develop the UI for the application using the Web Service Data Control we created in this part. The source of the sample can be downloaded from GitHub. The link to it will be provided with part 2.