Friday, September 4, 2015

ADF Application for Consuming REST JSON Service

ADF does not support to consume the JSON response from REST service and creating the Data Control from it. Bit, we can write java code and consume the REST service in it. Once we have the response, assign it to POJO class and create data control on top of java class.


Response:-
{
    "postalcodes": [
        {
            "adminName2": "Pathanamthitta",
            "adminName3": "Adoor",
            "postalcode": "689694",
            "adminCode1": "13",
            "countryCode": "IN",
            "lng": 76.8981,
            "placeName": "Kalanjoor",
            "lat": 9.06969,
            "adminName1": "Kerala"
        },
        {
            "adminName2": "Pathanamthitta",
            "adminName3": "Adoor",
            "postalcode": "689694",
            "adminCode1": "13",
            "countryCode": "IN",
            "lng": 76.8981,
            "placeName": "Mancode",
            "lat": 9.06969,
            "adminName1": "Kerala"
        },
        {
            "adminName2": "Pathanamthitta",
            "adminName3": "Adoor",
            "postalcode": "689694",
            "adminCode1": "13",
            "countryCode": "IN",
            "lng": 76.8981,
            "placeName": "Padam",
            "lat": 9.06969,
            "adminName1": "Kerala"
        }
    ]
}

Create an ADF Application and create a Java class in Model project.

Example :-

package com.rest.service.model;

public class RestCallService {
    public RestCallService() {
        super();
    }
}

Next create a POJO class for ‘PostCodes’ in the JSON response.

Example:-

package com.rest.service.model;

public class PostalCode {
    private String adminCode3;
    private String adminName2;
    private String adminName3;
    private String adminCode2;
    private String adminName1;
    private String postalcode;
    private String adminCode1;
    private String countryCode;
    private String lng;
    private String lat;   
    private String placeName;
   
    public PostalCode() {
        super();
    }
    public void setAdminCode3(String adminCode3) {
        this.adminCode3 = adminCode3;
    }
    public String getAdminCode3() {
        return adminCode3;
    }
    public void setAdminName2(String adminName2) {
        this.adminName2 = adminName2;
    }
    public String getAdminName2() {
        return adminName2;
    }

    public void setAdminName3(String adminName3) {
        this.adminName3 = adminName3;
    }
    public String getAdminName3() {
        return adminName3;
    }
    public void setAdminCode2(String adminCode2) {
        this.adminCode2 = adminCode2;
    }
    public String getAdminCode2() {
        return adminCode2;
    }
    public void setAdminName1(String adminName1) {
        this.adminName1 = adminName1;
    }
    public String getAdminName1() {
        return adminName1;
    }
    public void setPostalcode(String postalcode) {
        this.postalcode = postalcode;
    }
    public String getPostalcode() {
        return postalcode;
    }
    public void setAdminCode1(String adminCode1) {
        this.adminCode1 = adminCode1;
    }
    public String getAdminCode1() {
        return adminCode1;
    }
    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }
    public String getCountryCode() {
        return countryCode;
    }
    public void setLng(String lng) {
        this.lng = lng;
    }
    public String getLng() {
        return lng;
    }
    public void setLat(String lat) {
        this.lat = lat;
    }
    public String getLat() {
        return lat;
    }
    public void setPlaceName(String placeName) {
        this.placeName = placeName;
    }
    public String getPlaceName() {
        return placeName;
    }
}

Now, please add the code for consuming JSON Response with below code.

package com.rest.service.model;

import com.google.gson.Gson;

import com.google.gson.internal.LinkedTreeMap;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;

import java.util.ArrayList;
import java.util.List;

public class RestCallService {
    public RestCallService() {
        super();
    }
   
    public List<PostalCode> getPostalCodes() {
        List<PostalCode> postalCodes=new ArrayList<PostalCode>();
        try {
            URL url =
                new URL("http://api.geonames.org/postalCodeLookupJSON?postalcode=689694&country=IN&username=demo");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Content-Type", "application/json");
            BufferedReader in =
                new BufferedReader(new InputStreamReader(conn.getInputStream()));
            Gson g1 = new Gson();
            LinkedTreeMap fromJson = g1.fromJson(in, LinkedTreeMap.class);
            List<LinkedTreeMap> o =
                (ArrayList<LinkedTreeMap>)fromJson.get("postalcodes");
           System.out.println(fromJson);
            if (fromJson != null && o != null) {
                for (LinkedTreeMap node : o) {
                    PostalCode post=new PostalCode();
                    if (node != null && node.get("adminCode1") != null) {
                        post.setAdminCode1(node.get("adminCode1").toString());
                    }
                    if (node != null && node.get("adminName1") != null) {
                        post.setAdminName1(node.get("adminName1").toString());
                    }
                    if (node != null && node.get("adminCode2") != null) {
                        post.setAdminCode2(node.get("adminCode2").toString());
                    }
                    if (node != null && node.get("adminName2") != null) {
                        post.setAdminName2(node.get("adminName2").toString());
                    }
                    if (node != null && node.get("adminCode3") != null) {
                        post.setAdminCode3(node.get("adminCode3").toString());
                    }
                    if (node != null && node.get("adminName3") != null) {
                        post.setAdminName3(node.get("adminName3").toString());
                    }
                    if (node != null && node.get("postalcode") != null) {
                        post.setPostalcode(node.get("postalcode").toString());
                    }
                    if (node != null && node.get("countryCode") != null) {
                        post.setCountryCode(node.get("countryCode").toString());
                    }
                    if (node != null && node.get("lng") != null) {
                        post.setLng(node.get("lng").toString());
                    }
                    if (node != null && node.get("lat") != null) {
                        post.setLat(node.get("lat").toString());
                    }
                    if (node != null && node.get("placeName") != null) {
                        post.setPlaceName(node.get("placeName").toString());
                    }
                    postalCodes.add(post);
                }
                in.close();
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return postalCodes;
    }
    public static void main(String[] args) {
        RestCallService service=new RestCallService();
        service.getPostalCodes();
    }
}

Now, right click on the Java class – “RestCallService.java” and select “Create Data Control”

Go to ViewController project and create a jspx page. Go to Data Controls sections and refresh the section. Drag and drop postalCodes from Data Control and select the option Table à ADF Read Only Table.

Add the below properties to the table to make it pagination.

autoHeightRows="0" scrollPolicy="page"

Go to the Binding section of the jspx page and select “postalCodesIterator” and select RangeSize = 5 or whatever desired size.

Example :-

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <f:view>
    <af:document id="d1">
      <af:messages id="m1"/>
      <af:form id="f1">
        <af:panelGroupLayout id="pgl1" layout="vertical">
          <af:table value="#{bindings.postalCodes.collectionModel}" var="row"
                    rows="#{bindings.postalCodes.rangeSize}"
                    emptyText="#{bindings.postalCodes.viewable ? 'No data to display.' : 'Access Denied.'}"
                    fetchSize="#{bindings.postalCodes.rangeSize}"
                    rowBandingInterval="0"
                    selectedRowKeys="#{bindings.postalCodes.collectionModel.selectedRow}"
                    selectionListener="#{bindings.postalCodes.collectionModel.makeCurrent}"
                    rowSelection="single" id="t1" autoHeightRows="0"
                    scrollPolicy="page">
            <af:column sortProperty="#{bindings.postalCodes.hints.adminCode3.name}"
                       sortable="false"
                       headerText="#{bindings.postalCodes.hints.adminCode3.label}"
                       id="c1">
              <af:inputText value="#{row.bindings.adminCode3.inputValue}"
                            label="#{bindings.postalCodes.hints.adminCode3.label}"
                            required="#{bindings.postalCodes.hints.adminCode3.mandatory}"
                            columns="#{bindings.postalCodes.hints.adminCode3.displayWidth}"
                            maximumLength="#{bindings.postalCodes.hints.adminCode3.precision}"
                            shortDesc="#{bindings.postalCodes.hints.adminCode3.tooltip}"
                            id="it9">
                <f:validator binding="#{row.bindings.adminCode3.validator}"/>
              </af:inputText>
            </af:column>
            <af:column sortProperty="#{bindings.postalCodes.hints.adminName2.name}"
                       sortable="false"
                       headerText="#{bindings.postalCodes.hints.adminName2.label}"
                       id="c3">
              <af:inputText value="#{row.bindings.adminName2.inputValue}"
                            label="#{bindings.postalCodes.hints.adminName2.label}"
                            required="#{bindings.postalCodes.hints.adminName2.mandatory}"
                            columns="#{bindings.postalCodes.hints.adminName2.displayWidth}"
                            maximumLength="#{bindings.postalCodes.hints.adminName2.precision}"
                            shortDesc="#{bindings.postalCodes.hints.adminName2.tooltip}"
                            id="it10">
                <f:validator binding="#{row.bindings.adminName2.validator}"/>
              </af:inputText>
            </af:column>
            <af:column sortProperty="#{bindings.postalCodes.hints.adminName3.name}"
                       sortable="false"
                       headerText="#{bindings.postalCodes.hints.adminName3.label}"
                       id="c4">
              <af:inputText value="#{row.bindings.adminName3.inputValue}"
                            label="#{bindings.postalCodes.hints.adminName3.label}"
                            required="#{bindings.postalCodes.hints.adminName3.mandatory}"
                            columns="#{bindings.postalCodes.hints.adminName3.displayWidth}"
                            maximumLength="#{bindings.postalCodes.hints.adminName3.precision}"
                            shortDesc="#{bindings.postalCodes.hints.adminName3.tooltip}"
                            id="it4">
                <f:validator binding="#{row.bindings.adminName3.validator}"/>
              </af:inputText>
            </af:column>
            <af:column sortProperty="#{bindings.postalCodes.hints.adminCode2.name}"
                       sortable="false"
                       headerText="#{bindings.postalCodes.hints.adminCode2.label}"
                       id="c2">
              <af:inputText value="#{row.bindings.adminCode2.inputValue}"
                            label="#{bindings.postalCodes.hints.adminCode2.label}"
                            required="#{bindings.postalCodes.hints.adminCode2.mandatory}"
                            columns="#{bindings.postalCodes.hints.adminCode2.displayWidth}"
                            maximumLength="#{bindings.postalCodes.hints.adminCode2.precision}"
                            shortDesc="#{bindings.postalCodes.hints.adminCode2.tooltip}"
                            id="it1">
                <f:validator binding="#{row.bindings.adminCode2.validator}"/>
              </af:inputText>
            </af:column>
            <af:column sortProperty="#{bindings.postalCodes.hints.adminName1.name}"
                       sortable="false"
                       headerText="#{bindings.postalCodes.hints.adminName1.label}"
                       id="c8">
              <af:inputText value="#{row.bindings.adminName1.inputValue}"
                            label="#{bindings.postalCodes.hints.adminName1.label}"
                            required="#{bindings.postalCodes.hints.adminName1.mandatory}"
                            columns="#{bindings.postalCodes.hints.adminName1.displayWidth}"
                            maximumLength="#{bindings.postalCodes.hints.adminName1.precision}"
                            shortDesc="#{bindings.postalCodes.hints.adminName1.tooltip}"
                            id="it6">
                <f:validator binding="#{row.bindings.adminName1.validator}"/>
              </af:inputText>
            </af:column>
            <af:column sortProperty="#{bindings.postalCodes.hints.postalcode.name}"
                       sortable="false"
                       headerText="#{bindings.postalCodes.hints.postalcode.label}"
                       id="c7">
              <af:inputText value="#{row.bindings.postalcode.inputValue}"
                            label="#{bindings.postalCodes.hints.postalcode.label}"
                            required="#{bindings.postalCodes.hints.postalcode.mandatory}"
                            columns="#{bindings.postalCodes.hints.postalcode.displayWidth}"
                            maximumLength="#{bindings.postalCodes.hints.postalcode.precision}"
                            shortDesc="#{bindings.postalCodes.hints.postalcode.tooltip}"
                            id="it2">
                <f:validator binding="#{row.bindings.postalcode.validator}"/>
              </af:inputText>
            </af:column>
            <af:column sortProperty="#{bindings.postalCodes.hints.adminCode1.name}"
                       sortable="false"
                       headerText="#{bindings.postalCodes.hints.adminCode1.label}"
                       id="c11">
              <af:inputText value="#{row.bindings.adminCode1.inputValue}"
                            label="#{bindings.postalCodes.hints.adminCode1.label}"
                            required="#{bindings.postalCodes.hints.adminCode1.mandatory}"
                            columns="#{bindings.postalCodes.hints.adminCode1.displayWidth}"
                            maximumLength="#{bindings.postalCodes.hints.adminCode1.precision}"
                            shortDesc="#{bindings.postalCodes.hints.adminCode1.tooltip}"
                            id="it8">
                <f:validator binding="#{row.bindings.adminCode1.validator}"/>
              </af:inputText>
            </af:column>
            <af:column sortProperty="#{bindings.postalCodes.hints.countryCode.name}"
                       sortable="false"
                       headerText="#{bindings.postalCodes.hints.countryCode.label}"
                       id="c9">
              <af:inputText value="#{row.bindings.countryCode.inputValue}"
                            label="#{bindings.postalCodes.hints.countryCode.label}"
                            required="#{bindings.postalCodes.hints.countryCode.mandatory}"
                            columns="#{bindings.postalCodes.hints.countryCode.displayWidth}"
                            maximumLength="#{bindings.postalCodes.hints.countryCode.precision}"
                            shortDesc="#{bindings.postalCodes.hints.countryCode.tooltip}"
                            id="it3">
                <f:validator binding="#{row.bindings.countryCode.validator}"/>
              </af:inputText>
            </af:column>
            <af:column sortProperty="#{bindings.postalCodes.hints.lng.name}"
                       sortable="false"
                       headerText="#{bindings.postalCodes.hints.lng.label}"
                       id="c6">
              <af:inputText value="#{row.bindings.lng.inputValue}"
                            label="#{bindings.postalCodes.hints.lng.label}"
                            required="#{bindings.postalCodes.hints.lng.mandatory}"
                            columns="#{bindings.postalCodes.hints.lng.displayWidth}"
                            maximumLength="#{bindings.postalCodes.hints.lng.precision}"
                            shortDesc="#{bindings.postalCodes.hints.lng.tooltip}"
                            id="it11">
                <f:validator binding="#{row.bindings.lng.validator}"/>
              </af:inputText>
            </af:column>
            <af:column sortProperty="#{bindings.postalCodes.hints.lat.name}"
                       sortable="false"
                       headerText="#{bindings.postalCodes.hints.lat.label}"
                       id="c10">
              <af:inputText value="#{row.bindings.lat.inputValue}"
                            label="#{bindings.postalCodes.hints.lat.label}"
                            required="#{bindings.postalCodes.hints.lat.mandatory}"
                            columns="#{bindings.postalCodes.hints.lat.displayWidth}"
                            maximumLength="#{bindings.postalCodes.hints.lat.precision}"
                            shortDesc="#{bindings.postalCodes.hints.lat.tooltip}"
                            id="it7">
                <f:validator binding="#{row.bindings.lat.validator}"/>
              </af:inputText>
            </af:column>
            <af:column sortProperty="#{bindings.postalCodes.hints.placeName.name}"
                       sortable="false"
                       headerText="#{bindings.postalCodes.hints.placeName.label}"
                       id="c5">
              <af:inputText value="#{row.bindings.placeName.inputValue}"
                            label="#{bindings.postalCodes.hints.placeName.label}"
                            required="#{bindings.postalCodes.hints.placeName.mandatory}"
                            columns="#{bindings.postalCodes.hints.placeName.displayWidth}"
                            maximumLength="#{bindings.postalCodes.hints.placeName.precision}"
                            shortDesc="#{bindings.postalCodes.hints.placeName.tooltip}"
                            id="it5">
                <f:validator binding="#{row.bindings.placeName.validator}"/>
              </af:inputText>
            </af:column>
          </af:table>
        </af:panelGroupLayout>
      </af:form>
    </af:document>
  </f:view>
</jsp:root>


Run the application and can see the response is showing in the table with pagination.

1 comment:

  1. Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updatingmulesoft online training

    ReplyDelete