Java

Use Contentstack to power content for your Java projects

Create Java-based applications and use the Java SDK to fetch and deliver content from Contentstack. The Java SDK uses the Content Delivery APIs.

Getting Started

This guide will help you get started with our Java SDK to build apps powered by Contentstack.

Prerequisites

To get started with Java SDK, you will need an IDE. You can use an IDE of your choice, but make sure it supports Java.

SDK Installation and Setup

  1. Download the Java SDK and extract the ZIP file to your local disk.
  2. Add references/dependencies using Eclipse:
    1. Copy the 'Contentstack-x.x.x.jar' file into your project's libs folder.
    2. Add the dependency code into your 'build.gradle' file.
    3. Compile fileTree(dir: ‘libs’, include:[‘*.jar’])

Note: You can use this JAR file as a library in any Java project.

Initialize SDK

To initialize the SDK, you will need to specify the API Key, Delivery Token, and Environment Name of your stack.

Stack stack = Contentstack.stack("siteApiKey", "deliveryToken", "enviroment_name");

Once you have initialized the SDK, you can query entries to fetch the required content in your app.

Basic Queries

Get a Single Entry

To retrieve a single entry from a content type, use the code snippet given below:

ContentType contentType = stack.contentType("content_type_uid");
Entry blogEntry = contentType.entry("entry_uid");
blogEntry.fetch(new EntryResultCallBack() {
    @Override
    public void onCompletion(ResponseType responseType, Error error) {
        if (error == null) {
            // Success block
        } else {
            // Error block 
        }
    }
});

Get Multiple Entries

To retrieve multiple entries of a particular content type, use the code snippet given below:

//stack is an instance of Stack class
Query blogQuery = stack.contentType("content_type_uid").query();
blogQuery.find(new QueryResultsCallBack() {
    @Override
    public void onCompletion(ResponseType responseType, QueryResult queryResult, Error error) {
        if (error == null) {
            //Success block
        } else {
            //Error block
        }
    }
});

These were example of some of the basic queries of the SDK. For advanced queries, refer to our API reference documentation by visiting the link given below.

API Reference

Go through our Java API Reference guide to know about the methods that can be used to query your content in Contentstack.

Read Java API Reference Guide

Download SDK

The Java SDK interacts with and uses our Content Delivery APIs to fetch content from Contentstack. Download the SDK to deliver content to your Java projects. 

Download the Java SDK

Example

To help you get started, we have provided Java code snippet below. This snippet shows how to make queries and fetch entries from Contentstack. 

public class News {

    public static void main(String[] args) {
        Stack stack = null;
        try {
            stack = Contentstack.stack("blt920bb7e90248f607", "blt0c4300391e033d4a59eb2857", "production");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        Query query = stack.contentType("news").query();
        query.find(new QueryResultsCallBack() {
            @Override
            public void onCompletion(ResponseType responseType, QueryResult queryResult, com.builtio.contentstack.Error error) {
                if (error == null) {
                    for (Entry entry: queryResult.getResultObjects()) {
                        System.out.println("Title: " + entry.getString("title"));
                    }
                } else {
                    System.out.println("Failed: " + error.getErrorCode());
                }

            }
        });
    }
}

Result:

Title: The Martian Movie Review Ticket Received
Title: मंगल ग्रह का निवासी
Title: Uber भारत निधि के लिए $ 2100000000 जुटाने का लक्ष्य रखा, चीन विस्तार
Title: Uber aims to raise $2.1 billion to fund India, China expansion 123456
Title: Uber aims to raise $2.1 billion to fund India, China expansion 123456
Title: Dhoni to play charitable golf event in US
Title: Indian economy offers hope as China struggles
Title: चीन संघर्ष के रूप में भारतीय अर्थव्यवस्था आशा प्रदान करता है
Title: Indian economy offers hope as China struggles


Was this article helpful?
top-arrow