Get Started with Java Delivery 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>{version}</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:{version}'

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

You can download the latest dependency version here.

Initialize SDK

Contentstack offers three regions (North America (NA), Europe (EU) and Azure North America (NA) as data centers to store customers' account details and data. These 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 Europe and Azure NA 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");

Note: By default, the SDK uses the North American region. Configuration changes are not required for North American region users.

For Europe or Azure North America, check the code of your region and configure your SDK.

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

For Setting the branch for Europe or Azure North America, check the code of your region and initialize SDK in a particular branch.

Basic Queries

Contentstack SDKs let you interact with the Content Delivery APIs and retrieve content from Contentstack. They are read-only in nature. The SDKs fetch and deliver content from the nearest server via Fastly, our powerful and robust CDN.

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

Note: Currently, the Java SDK does not support multiple content types referencing in a single query. For more information on how to query entries and assets, refer the Queries section of our Content Delivery API documentation.

Paginating Responses

In a single instance, the Get Multiple Entries query will retrieve only the first 100 items of the specified content type. You can paginate and retrieve the rest of the items in batches using the skip and limit parameters in subsequent requests.

Stack stack = Contentstack.stack("stack_api_key", "delivery_token", "environment", false);
Query csQuery = stack.contentType("contentType_name").query();
csQuery.skip(20);
csQuery.limit(20);
csQuery.find(new QueryResultsCallBack() {

          @Override
          public void onCompletion(ResponseType responseType, QueryResult queryResult, Error error) {
          }
      });

Limitations

  • We have a URL size limitation of 8KB on API Requests that hit our CDN services. Any Request URL that goes above this size limit will receive the 400 - Bad request error response. Please make sure you limit the size of your API Requests.
  • The Java SDK does not support multiple content types referencing in a single query.
  • Currently, the Java SDK does not yet support querying Global Field schemas (All Global Fields and Single Global Field). You can include these details when querying content type details (All Content Types and Single Content Type) with the include_global_field_schema query parameter.

More Resources

Was this article helpful?

Thanks for your feedbackSmile-icon

On This Page

^