OpenSource REST API URL and Retrieving Data From it By Using Spring MVC
Last Updated :
02 May, 2022
In this internet era, a lot of helper services are available in the form of REST API and mostly REST API provides us the details in the form of JSON/XML. We can use them in our applications and render the data as we like. Here is the list of a few opensource REST API URLs:
API Name | Description | URL |
---|
CoinGecko | Exchange rates | https://api.coingecko.com/api/v3/exchange_rates |
ExchangeRate-API | Exchange rates | https://open.er-api.com/v6/latest/INR |
Genderize.io | Prediction of gender based on name | https://api.genderize.io?name=rachel |
WeatherDB | To know about the weather | https://weatherdbi.herokuapp.com/data/weather/london |
Zippopotamus | Zipcode information | https://api.zippopotam.us/IN/600028 |
There are still more open-source APIs available. Here we will be learning how to extract the zip code details using Spring MVC:
REST API: http://api.zippopotam.us/IN/<Required Pincode>
Example:
http://api.zippopotam.us/IN/600028
Output: JSON
{
"post code": "600028",
"country": "India",
"country abbreviation": "IN",
"places": [
{
"place name": "Fore Shore Estate",
"longitude": "80.2417",
"state": "Tamil Nadu",
"state abbreviation": "TN",
"latitude": "13.0206"
},
{
"place name": "Ramakrishnanagar",
"longitude": "80.2417",
"state": "Tamil Nadu",
"state abbreviation": "TN",
"latitude": "13.0206"
},
{
"place name": "R A Puram Colony",
"longitude": "80.2417",
"state": "Tamil Nadu",
"state abbreviation": "TN",
"latitude": "13.0206"
}
]
}
Now using Spring MVC Framework, let us try to render the REST API details on the screen
Implementation:
It is as depicted Step by Step below where initially let us do see the project structure which is as follows:
A. File: pom.xml
XML
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zipCode.zipCode_RestAPI</groupId>
<artifactId>zipCode_RestAPI</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>profileGenerator</name>
<url>http://maven.apache.org</url>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
<spring-version>5.1.0.RELEASE</spring-version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- JSTL Dependency -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- Servlet Dependency -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- JSP Dependency -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>Zipcode</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.0-beta-1</version>
</plugin>
</plugins>
</build>
</project>
B. File: index.jsp
This is the page that gets rendered when the project is deployed in tomcat
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ZipCode</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<style type="text/css">
.main-form, .profile-area {
width: 500px;
background: #FF5733;
}
.main-form {
margin: 50px auto 0px;
}
.profile-area {
margin: 10px auto;
}
.main-form section, .profile-area section {
margin-bottom: 15px;
background: #f7f7f7;
box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
}
.main-form section {
padding: 30px;
}
.profile-area section {
padding: 30px 30px 30px;
}
.profile-area section > div {
text-align: center;
}
.main-form h3 {
margin: 0 0 15px;
}
.form-control, .btn {
min-height: 38px;
border-radius: 2px;
}
.btn {
font-size: 15px;
font-weight: bold;
}
.hideElement {
display: none;
}
</style>
</head>
<body>
<div class="main-form" id="main-form">
<section>
<h5 class="text-center">Enter your zipcode</h5>
<div class="form-group">
<input id="zipcode" type="text" class="form-control" placeholder="Enter zipcode here..." required="required">
</div>
<div class="form-group">
<button onclick="loadData()" class="btn btn-primary btn-block">Find Area Details</button>
</div>
</section>
</div>
<div class="profile-area hideElement" id="profile-area">
<section>
<div id="loader" class="hideElement">
<div class="spinner-border" role="status">
<span class="sr-only">Loading...</span>
</div>
</div>
<div id="profile" class="hideElement">
<br><br>
<p><strong>Country : </strong><span id="country"></span></p>
<p><strong>State : </strong><span id="statename"></span></p>
<p><strong>Localities : </strong><span id="associatedplaces"></span></p>
<p><strong>Latitude : </strong><span id="associatedlatitude"></span></p>
<p><strong>Longitude : </strong><span id="associatedlongitude"></span></p>
</div>
</section>
</div>
</body>
<script>
function loadData() {
document.getElementById("profile-area").classList.remove("hideElement");
document.getElementById("loader").classList.remove("hideElement");
document.getElementById("profile").classList.add("hideElement");
var zipCode = document.getElementById("zipcode").value;
if(zipCode != "" && zipCode != null) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var jsonResponse = JSON.parse(this.responseText);
document.getElementById("country").innerHTML = jsonResponse.country;
document.getElementById("statename").innerHTML = jsonResponse.statename;
document.getElementById("associatedplaces").innerHTML = jsonResponse.associatedplaces;
document.getElementById("associatedlatitude").innerHTML = jsonResponse.associatedlatitude;
document.getElementById("associatedlongitude").innerHTML = jsonResponse.associatedlongitude;
document.getElementById("loader").classList.add("hideElement");
document.getElementById("profile").classList.remove("hideElement");
}
};
xhttp.open("GET", "getLocalityDetailsByZipCode?zipCode=" + zipCode, true);
xhttp.send();
console.log("done");
} else {
console.log("Enter zipcode...")
}
}
</script>
</html>
Output:
On entering any valid zip code. For example 600028, name in the input box, we will get the below details.
Execution Process: From the JSP page, a "GET" method is invoked with the search string and it is redirected to the Spring Controller class, where the REST API URL, <a target="_blank" rel="noopener noreferrer nofollow" href="http://api.zippopotam.us/IN/http://api.zippopotam.us/IN/<valid zipcode> is invoked and it will create the JSON object as given above, From that, we are taking "country", "state","latitude","longitude" and "localities" values.
Country : India
State : Tamil Nadu
Localities : [Fore Shore Estate, Ramakrishnanagar, R A Puram Colony]
Latitude : [13.0206, 13.0206, 13.0206]
Longitude : [80.2417, 80.2417, 80.2417]
Output: Screen
Required code for retrieving the data and parsing the JSON output is as follows. Important necessary files to achieve the flow
File: AppConfig.java
Java
// Java Program to Illustrate Application
// configuration Class
// Importing required classes
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
// Annotations
@Configuration
@EnableWebMvc
@ComponentScan(basePackages
= { "com.zipCode.zipCode_RestAPI" })
// Class
public class AppConfig {
@Bean public InternalResourceViewResolver resolver()
{
InternalResourceViewResolver resolver
= new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/");
resolver.setSuffix(".jsp");
return resolver;
}
}
File: SpringMvcDispatcherServletInitializer.java
Java
// Java Program to Illustrate
// SpringMvcDispatcherServletInitializer Class
// Importing required classes
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
// Class
public class SpringMvcDispatcherServletInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
// Method 1
@Override protected Class<?>[] getRootConfigClasses()
{
return null;
}
// Method 2
@Override protected Class<?>[] getServletConfigClasses()
{
return new Class[] { AppConfig.class };
}
// Method 3
@Override protected String[] getServletMappings()
{
return new String[] { "/" };
}
}
File: ZipCodeController.java
Java
// Java Program to Illustrate ZipCodeController Class
// Importing required classes
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
// Annotation
@Controller
// Class
public class ZipCodeController {
// Method 1
@RequestMapping("/getLocalityDetailsByZipCode")
public @ResponseBody
JsonObject getLocalityDetailsByZipCode(String zipCode)
throws IOException
{
JsonObject jsonObject = new JsonObject();
jsonObject = getLocalityDetailsByZip(zipCode);
// Parsing the JSONObject
JsonObject finalJsonObject = new JsonObject();
String country
= jsonObject.get("country").toString();
country = country.replaceAll("^\"|\"$", "");
JsonArray jsonPlacesArray = null;
ArrayList placesList = new ArrayList();
ArrayList stateList = new ArrayList();
ArrayList latitudeList = new ArrayList();
ArrayList longitudeList = new ArrayList();
// Places data is available as JSONArray
jsonPlacesArray
= jsonObject.get("places").getAsJsonArray();
// It is iterated
Iterator<JsonElement> objectIterator
= jsonPlacesArray.iterator();
while (objectIterator.hasNext()) {
JsonElement object = objectIterator.next();
JsonObject jObj = object.getAsJsonObject();
System.out.println(
jObj.get("place name").toString()
+ jObj.get("state").toString());
placesList.add(jObj.get("place name")
.toString()
.replaceAll("^\"|\"$", ""));
stateList.add(
jObj.get("state").toString().replaceAll(
"^\"|\"$", ""));
latitudeList.add(
jObj.get("latitude")
.toString()
.replaceAll("^\"|\"$", ""));
longitudeList.add(
jObj.get("longitude")
.toString()
.replaceAll("^\"|\"$", ""));
}
finalJsonObject.addProperty("country", country);
finalJsonObject.addProperty("associatedplaces",
placesList.toString());
finalJsonObject.addProperty("associatedplacessize",
placesList.size());
finalJsonObject.addProperty("state",
stateList.toString());
finalJsonObject.addProperty(
"statename", stateList.get(0).toString());
finalJsonObject.addProperty(
"associatedlatitude", latitudeList.toString());
finalJsonObject.addProperty(
"associatedlatitudesize", latitudeList.size());
finalJsonObject.addProperty(
"associatedlongitude",
longitudeList.toString());
finalJsonObject.addProperty(
"associatedlongitudesize",
longitudeList.size());
return finalJsonObject;
}
// Method 2
private JsonObject
getLocalityDetailsByZip(String zipCode)
throws IOException
{
String data = null;
StringBuilder responseData = new StringBuilder();
JsonObject jsonObject = null;
URL url = null;
url = new URL("http://api.zippopotam.us/IN/"
+ zipCode);
HttpURLConnection con
= (HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = con.getResponseCode();
System.out.println(
"\nSending 'GET' request to URL : " + url);
// System.out.println("Response Code : " +
// responseCode);
// Try block to check for exceptions
try (BufferedReader in
= new BufferedReader(new InputStreamReader(
con.getInputStream()))) {
String line;
while ((line = in.readLine()) != null) {
responseData.append(line);
}
jsonObject = new Gson().fromJson(
responseData.toString(), JsonObject.class);
}
// System.out.println(data);
return jsonObject;
}
}
Conclusion: This process of code is required for retrieving any REST API call. We need to know whether REST API provides either JSONObject/JSONArray and accordingly we have to parse the data.
Similar Reads
Spring MVC with MySQL and Junit - Finding Employees Based on Location
In real-world scenarios, organizations are existing in different localities. Employees are available in many locations. Sometimes they work in different 2 locations i.e. for a few days, they work on location 1 and for a few other days, they work on location 2. Let's simulate this scenario via MySQL
8 min read
Spring MVC - Get University/College Details via REST API
REpresentational State Transfer (REST) is an architectural style that defines a set of constraints to be used for creating web services. REST API is a way of accessing web services in a simple and flexible way without having any processing. Spring MVC is a Web MVC Framework for building web applicat
6 min read
Spring MVC - JSTL forEach Tag with Example
JSP Standard Tag Library (JSTL) is a set of tags that can be used for implementing some common operations such as looping, conditional formatting, and others. JSTL aims to provide an easy way to maintain SP pages The use of tags defined in JSTL has Simplified the task of the designers to create Web
6 min read
Spring MVC - Iterating List on JSP using JSTL
JSP Standard Tag Library (JSTL) is a set of tags that can be used for implementing some common operations such as looping, conditional formatting, and others. JSTL aims to provide an easy way to maintain SP pages The use of tags defined in JSTL has Simplified the task of the designers to create Web
6 min read
Two-Way Data Binding in Spring MVC with Example
Data Binding, as the name itself, is a self-explanatory word. In data binding what we have to do is we have to capture or store the data so that we can bind that data with another resource (for example displaying the data in the frontend part) as per our needs or we can also read the data from a var
7 min read
Spring MVC - Basic Example using JSTL
JSP Standard Tag Library (JSTL) is a set of tags that can be used for implementing some common operations such as looping, conditional formatting, and others. JSTL aims to provide an easy way to maintain SP pages The use of tags defined in JSTL has Simplified the task of the designers to create Web
8 min read
Spring MVC @ModelAttribute Annotation with Example
In Spring MVC, the @ModelAttribute annotation binds a method parameter or method return value to a named model attribute and then exposes it to a web view. It refers to the property of the Model object. For example, if we have a form with a form backing object that is called "Student" then we can ha
8 min read
Data Transfer Object (DTO) in Spring MVC with Example
In Spring Framework, Data Transfer Object (DTO) is an object that carries data between processes. When you're working with a remote interface, each call is expensive. As a result, you need to reduce the number of calls. The solution is to create a Data Transfer Object that can hold all the data for
7 min read
Spring MVC - Capture and Display the Data from Registration Form
This article is the continuation of this article Spring MVC - Create Registration Form using Form Tag Library, where we have successfully created a registration form using the Form Tag Library. Here in this article, we are going to explain how can we capture the data that are entered by the user and
3 min read
Spring MVC - Create Registration Form using Form Tag Library
Spring Framework provides springâs form tag library for JSP views in Springâs Web MVC framework. In Spring Framework, we use Java Server Pages(JSP) as a view component to interact with the user. From version 2.0, Spring Framework provides a comprehensive set of data binding-aware tags. These tags ar
7 min read