Blog / Development

Introducing the Kentico Cloud Delivery Java SDK


by Jan Lenoch

Oct 5, 2017

Kentico Cloud now has its full fledged Java SDK. Feature-wise, it is on a par with the .NET and other SDKs, right from the start. Let's see its core features and how you can use them.

Java is a fantastic ecosystem. Along with its sibling technology in the Microsoft world—.NET—it became a well-established platform for the most complex enterprise-level applications. We're pleased to announce that our great open-source contributor, Adam J. Weigold of TrustedChoice, authored the Delivery Java SDK, together with a code generator.

A Feature-Complete SDK

Yes, right off the bat, the SDK sports all the features that the .NET SDK has; like strongly typed content, runtime-typed content, and even one of our newest features—Taxonomy. Also, from day zero our Developer Hub was updated with Java code examples.

It is now very easy for you as a Java developer to jumpstart with Kentico Cloud.

How To Use It

Assuming you already have your Java development environment set up, there is just one step required—to establish a dependency to the Kentico Cloud Delivery Java SDK Maven entry.

<dependency>
    <groupId>com.kenticocloud</groupId>
    <artifactId>delivery-sdk-java</artifactId>
    <version>1.0.4</version>
</dependency>


To do that, you can  either edit the Gradle build system's configuration files, or use the Maven Project Object Model (POM). This is the required change in Gradle settings.

repositories {
	mavenCentral()
}

dependencies {
    compile('com.kenticocloud:delivery-sdk-java:1.0.4')
}


The Code Generator

Once you've set up the dependencies, chances are you already have a Kentico Cloud project to point your future Java application to. If this is the case, then it is time for another tool to serve—the code generator. As in other SDKs, the generator produces strongly typed Java model classes of Kentico Cloud content types.

The easiest way of using it is—again—via the Gradle build plugin.

plugins {
  id "com.kenticocloud.generator" version "1.1"
}


The configuration requires the identification of which project ID, package name, and output folder should be used.

apply plugin: 'java'

kenticoModel {
	projectId = '975bf280-fd91-488c-994c-2f04416e5ee3'
	packageName = 'com.dancinggoat.models'
	outputDir = file('generated-sources')
}

dependencies {
    compile('com.kenticocloud:delivery-sdk-java:1.0.3')
}


Then, just invoke the task.

./gradlew generateModels


Getting Kentico Cloud Content

Upon creating an instance of the DeliveryClient class, you can start querying and filtering for Kentico Cloud content items, content models, and other artifacts.

DeliveryClient client = new DeliveryClient("975bf280-fd91-488c-994c-2f04416e5ee3");


The query is built using the builder design pattern. You can chain various filtering methods of the DeliveryParameterBuilder class together. When done, you just append the call to the “build” method to get the final dictionary that's used as a parameter of the “getItem(s)” methods.

// Retrieves a list of the specified elements from the first 10 content items of
// the 'brewer' content type, ordered by the 'product_name' element value,
// in the Spanish language.
ContentItemsListingResponse response = client.getItems(
    DeliveryParameterBuilder.params()
        .language("es-ES")
        .filterEquals("system.type", "brewer")
        .projection("image", "price", "product_status", "processing")
        .page(null, 10)
        .orderByAsc("elements.product_name")
        .build();
);


To obtain a strongly typed content item, all you have to do is specify the type of  generated model as a parameter of the “getItem” method.

ArticleItem item = client.getItem("on_roasts", ArticleItem.class);


Should you wish to get a strongly typed content item without even specifying the type upfront, then this is how you do it.

Object itemObj = client.getItem("on_roasts", Object.class);


You'll get an instance of the “ArticleItem” class, just like in the previous example. It works in a similar way to the automatic typing at runtime in our .NET SDK.

Latest Bits Included

Kentico Cloud is a true headless CMS, hence it treats all programming languages as “first-class” ones. The Java SDK is a great example of that as it supports the newest API features off the cuff.

Do you wish to retrieve a taxonomy? Here's how.

TaxonomyGroup taxonomyGroup = client.getTaxonomyGroup("personas");


Do you want to be sure your app always gets the most up-to-date content? Kentico Cloud API can now enforce getting the most recent content, circumventing the CDN should that content have been updated a second ago. You can enforce that via the “setWaitForLoadingNewContent” method of the DeliveryOptions class.

DeliveryOptions deliveryOptions = new DeliveryOptions();
deliveryOptions.setProjectId(projectId);
deliveryOptions.setWaitForLoadingNewContent(true);
DeliveryClient client = new DeliveryClient(deliveryOptions);


Extensibility

Similar to our other SDKs, you can bring in your own implementation of the following:

  • resolving of links to other content items that appear in rich text elements
  • handling of  broken links
  • resolving (rendering) of modular content items in rich text elements

You can supply your code for the first two by implementing these interfaces:

The third one (modular content in rich text) can be implemented by inheriting from the InlineContentItemsResolver abstract class.

I Want All the Details!

If you already know Kentico Cloud and wish to learn more about the Java SDK, the best starting places are the README.md descriptions of both the SDK and the code generator. Then, you can dive into the wiki for even more details. The code examples reside either in our Developer Hub or—as test methods—in the repository of the SDK.

On the other hand, if you're not familiar with Kentico Cloud, it is best to go through the Using the Delivery API tutorial in our Developer Hub first. That way, you'll be able to quickly learn the fundamental concepts. These will be more than sufficient for you to start playing with the Java SDK.

As always, you can ask questions in our forums.

If you wish to meet the author of the SDK in person, reserve your seat at the Boston Kentico Roadshow.

Happy Java coding!