Get Started with Java SDK

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

Prerequisites

To get started with Java SDK, you will the following:

  • An IDE. You can use an IDE of your choice, but make sure it supports Java.
  • Java SDK version 1.8 or later

SDK Installation and Setup

Add the following dependency code snippets into your project:

  • Maven
    <dependency>
        <groupid>com.contentstack.sdk</groupid>
        <artifactid>java</artifactid>
        <version>1.4.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.4.0'

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

Initialize SDK

Contentstack offers two regions (US and European) as data centers to store customers' account details and data. Both regions are independent of each other and therefore have a dedicated set of instructions to use SDKs offered by Contentstack.

To use SDKs for the European region, you will have to make certain changes in the configuration of the SDK, as detailed below, and the rest of the instructions remain the same.

To initialize the SDK, you will need to specify the stack’s API Key, delivery token, and name of the environment where you will publish your content.

Stack stack = Contentstack.stack("stack_api_key", "delivery_token", "environment_name");

For Setting the European Region:

If you want to set European region, refer to the code below:

Config config = Config();
Config.region = ContentstackRegion.EU;
Stack stack = Contentstack.stack("stack_api_key", "delivery_token", "environment_name", config);

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 the Contentstack Java SDK API reference.

More articles in "Use Java SDK"

On This Page

top-arrow