Consume a REST Service in Oracle MAF

INTRODUCTION

REST-JSON is the recommended technology of choice for consuming web services in Oracle MAF. In this blog, we will build a MAF application that displays weather forecast for a chosen city. I am using the excellent OpenWeatherMap api to demonstrate consuming a rest service in a MAF mobile application. The application will allow you to select from a predefined list of cities and display the weather forecast in 3 three hour increments as supported by the api. When the application is completed and run in a simulator or device, this is what you should see

Screenshot_20170905-130232.png

SOLUTION

To consume a rest service we have to:
1. Create a new REST connection in Jdeveloper and add it to your application.
2. A Java class that creates a RestServiceAdapter and invokes the service.
3. Java classes that model the web service return object(s).

Create a new connection for your rest service as shown below:
restConnection.PNG

restConnectionDetails.PNG

Next, create a new java class for the RestServiceAdapter utility. In this case, we will be retrieving forecast data, so a simple get method should suffice. The sample code available for download does show examples of other rest verbs like update, put and delete.
The RestServiceAdapter api usage is shown below:

You can call this method using a simple cover method like:

The rest service forecast json should now be inspected to model our java classes. Take a look at this JSON:

This JSON describes a main container that has two internal parameters called cod and message that do not concern us. The cnt attribute will contain the number of forecast objects contained in the main “List” attribute. You can model the main container as the “AllForecast” java class.

Each item in the list attribute contains an hourly forecast for the city code the service was invoked for. The “Forecast” java class can be modeled as:

You can find the java classes for the remaining objects in the Forecast in the attached sample code. Once all the return JSON has been modeled as java classes, all that needs to be done is code the api call in your managed bean. Create a managed bean in the main adfc-mobile-config in application scope. This class will contain our api call like:

Note the call to JSONBeanSerializationHelper maf api to easily convert the JSON response to our java class AllForecast. Remember to sign up for an account on the OpenWeatherMap site for the api key that’s required for this to work.
Now, the bean attribute “forecast” can be used in the amx page to display the weather data.

Again, note that I coded the list view manually on the amx page. You can easily convert the AllForecast class to a data control and drop the forecast list to create an amx listview.

As an added bonus, I have added a drop down on the page to allow the user to select a city and display the associated weather forecast. You will have to find the appropriate city codes from the openweathermap site to add your cities to this page. You can find this in the attached sample code.

Starting with MAF 2.4, the AMPA library has been officially folded into the framework as “Client Data Model (CDM)”. You can use this api to call the rest service. This API has a jdeveloper wizard that helps you add the service and model the java classes. I find the JDeveloper wizard a bit too complex for use and makes some assumptions on your usecase. As of now, I still prefer the RestServiceAdapter approach for the flexibility it offers. I will try to document CDM in an upcoming blog.

DOWNLOAD

WeatherApp.zip
 

REFERENCES

Oracle MAF Developer Guide: Using Web Services in a MAF Application
OpenWeatherMap API: 5 day forecast API

vivek

vivek

Oracle Fusion Middleware Architect at FUSION Applied
Vivek is an Oracle Certified Fusion Middleware architect. He has over 18 years of experience implementing custom software solutions in the Financial, Health, and Government sectors. He is one of the founders of Fusion Applied.
vivek

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz