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

You can add the following dependency code snippets into your project:

  • Maven
    <dependency>
        <groupid>com.contentstack.sdk</groupid>
        <artifactid>java</artifactid>
        <version>1.2.0</version>
    </dependency>

    Maven users need to add the above code in your pom.xml file under the <dependencies> section.

  • Gradle
    compile 'com.contentstack.sdk:java:1.2.0'

    Gradle users need to add the above dependency code into your build.gradle file.

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("stack_API_key", "delivery_token", "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 examples 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

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

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, 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: Uber aims to raise $2.1 billion to fund India, China expansion
Title: The Martian Movie Review Ticket Received
Title: Dhoni to play charitable golf event in US
Title: Indian economy offers hope as China struggles
Was this article helpful?
top-arrow