Open In App

Spring MVC - Getting Cryptocurrency Details using REST API

Last Updated : 09 Oct, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Cryptocurrencies like Bitcoin (BTC), Ethereum (ETH), Litecoin (LTC), and Zcash (ZEC) are gaining global attention. Retrieving their live details is often required for applications such as trading platforms, dashboards, or analytics tools.

REST APIs are widely used to access cryptocurrency data due to their simplicity, scalability, and flexibility. In this article, we will demonstrate how to fetch cryptocurrency details using Spring MVC.

For example, calling the following REST API:

https://api.coincap.io/v2/assets/bitcoin

produces JSON output containing details like rank, symbol, and price in USD.

Screenshot-2025-10-07-114900
JSON output

Step-by-Step Implementation

The implementation uses a Maven-driven Spring MVC project. The process includes setting up project dependencies, configuring Spring MVC, creating a controller for REST API calls, and building a JSP page to display results.

Project Structure:

A standard Maven project structure is used with pom.xml managing all dependencies. Key dependencies include Spring Web MVC, JSTL, Gson, and Commons IO.

Project Structure
project structrue

pom.xml

XML
<!-- Example dependencies from pom.xml -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.6</version>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>javax.servlet.jsp.jstl-api</artifactId>
    <version>1.2.1</version>
</dependency>

Other configurations include compiler plugins and WAR packaging for deployment on a web server like Tomcat.

Spring Configuration

AppConfig.java: Sets up Spring MVC with component scanning and a view resolver for JSP pages.

Java
public class SpringMvcDispatcherServletInitializer 
        extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() { return null; }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { AppConfig.class };
    }

    @Override
    protected String[] getServletMappings() { return new String[] { "/" }; }
}

SpringMvcDispatcherServletInitializer.java: Initializes the DispatcherServlet.

Java
public class SpringMvcDispatcherServletInitializer 
        extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() { return null; }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { AppConfig.class };
    }

    @Override
    protected String[] getServletMappings() { return new String[] { "/" }; }
}

Controller for REST API

CryptocurrencyController.java: Handles API calls and parses JSON response.

Java
@Controller
public class CryptocurrencyController {

    @RequestMapping("/getCryptocurrencyDetailsByName")
    public @ResponseBody JsonObject getCryptocurrencyDetails(String cryptocurrency) throws IOException {
        JsonObject jsonObject = new JsonObject();
        String data = getCryptocurrencyData(cryptocurrency);

        data = data.replaceAll("^\"|\"$", "");
        StringTokenizer jsonTokenizer = new StringTokenizer(data, ",");
        String[] internalData;

        while (jsonTokenizer.hasMoreTokens()) {
            internalData = StringUtils.split(jsonTokenizer.nextToken(), ":");
            String key = internalData[0].replace("\"", "");
            String value = internalData[1].replace("\"", "");
            
            if ("rank".equalsIgnoreCase(key)) jsonObject.addProperty("rank", value);
            if ("symbol".equalsIgnoreCase(key)) jsonObject.addProperty("symbol", value);
            if ("priceUsd".equalsIgnoreCase(key)) jsonObject.addProperty("priceUsd", value);
        }

        return jsonObject;
    }

    private String getCryptocurrencyData(String cryptocurrency) throws IOException {
        URL url = new URL("https://api.coincap.io/v2/assets/" + cryptocurrency);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", "Mozilla/5.0");

        StringBuilder responseData = new StringBuilder();
        try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
            String line;
            while ((line = in.readLine()) != null) responseData.append(line);
        }

        JsonObject jsonObject = new Gson().fromJson(responseData.toString(), JsonObject.class);
        return jsonObject.get("data").toString();
    }
}

JSP Page

findCryptoDetails.jsp: provides a simple interface for users to enter the cryptocurrency name and view its details.

HTML
<input id="cryptocurrency" type="text" class="form-control" placeholder="Enter cryptocurrency">
<button onclick="loadData()" class="btn btn-primary">Find Cryptocurrency Details</button>

JavaScript handles the API call and updates the HTML elements with the fetched data.

Key features:

  • Input box for cryptocurrency name
  • Button to trigger the REST API call using XMLHttpRequest
  • Displays Rank, Symbol, and Price(USD) dynamically

Output:

Output
We need to give cryptocurrency value here. It can be bitcoin or ethereum etc.

Let us check for bitcoin

Output
Bitcoin

Article Tags :

Explore