Skip to content

contentstack/contentstack-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Contentstack

Java SDK for Contentstack

Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. Build your application frontend, and Contentstack will take care of the rest. Read More.

Contentstack provides Java SDK to build application on top of Java. Given below is the detailed guide and helpful resources to get started with our Java SDK.

Prerequisite

You will need JDK installed on your machine. You can install it from here.

Setup and Installation

To use the Contentstack Java SDK to your existing project, perform the steps given below:

Group Id: com.contentstack.sdk

Artifact Id: java

LATEST VERSION

Maven

<dependency>
  <groupId>com.contentstack.sdk</groupId>
  <artifactId>java</artifactId>
  <version>{version}</version>
</dependency>

Gradle

implementation 'com.contentstack.sdk:java:{version}'

Get updated version from version

Key Concepts for using Contentstack

Stack

A stack is like a container that holds the content of your app. Learn more about Stacks.

Content Type

Content type lets you define the structure or blueprint of a page or a section of your digital property. It is a form-like page that gives Content Managers an interface to input and upload content. Read more.

Entry

An entry is the actual piece of content created using one of the defined content types. Learn more about Entries.

Asset

Assets refer to all the media files (images, videos, PDFs, audio files, and so on) uploaded to Contentstack. These files can be used in multiple entries. Read more about Assets.

Environment

A publishing environment corresponds to one or more deployment servers or a content delivery destination where the entries need to be published. Learn how to work with Environments.

Contentstack Java SDK: 5-minute Quickstart

Initializing your SDK

To initialize the SDK, specify application API key, access token, and environment name of the stack as shown in the snippet given below:

Stack stack=Contentstack.stack("apiKey","accessToken","environment");

To get the API credentials mentioned above, log in to your Contentstack account and then in your top panel navigation, go to Settings > Stack to view the API Key and Access Token.

Querying content from your stack

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

//stack is an instance of Stack class
ContentType contentType = stack.contentType("content_type_uid");
Entry entry = contentType.entry("entry_uid");
entry.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 query = stack.contentType("content_type_uid").query();
query.find(new QueryResultsCallBack(){
@Override
public void onCompletion(ResponseType responseType, QueryResult queryResult, Error error) {
        if(error == null){
           //Success block
        }else{
           //Error block
    }}
});

Advanced Queries

You can query for content types, entries, assets and more using our Java API Reference.

Java API Reference Doc

Working with Images

We have introduced Image Delivery APIs that let you retrieve images and then manipulate and optimize them for your digital properties. It lets you perform a host of other actions such as crop, trim, resize, rotate, overlay, and so on.

For example, if you want to crop an image (with width as 300 and height as 400), you simply need to append query parameters at the end of the image URL, such as, https://images.contentstack.io/v3/assets/download?crop=300,400. There are several more parameters that you can use for your images.

Read Image Delivery API documentation.

You can use the Image Delivery API functions in this SDK as well. Here are a few examples of its usage in the SDK.

//set the image quality to 100
LinkedHashMap imageParams = new LinkedHashMap();
imageParams.put("quality", 100);
imageUrl = Stack.ImageTransform(imageUrl, imageParams);

//resize the image by specifying width and height
LinkedHashMap imageParams = new LinkedHashMap();
imageParams.put("width", 100);
imageParams.put("height",100);
imageUrl = Stack.ImageTransform(imageUrl, imageParams);

//enable auto optimization for the image
LinkedHashMap imageParams = new LinkedHashMap();
imageParams.put("auto", "webp");
imageUrl = Stack.ImageTransform(imageUrl, imageParams);

Helpful Links