This guide will help you get started with Contentstack Java Utils SDK to build apps powered by Contentstack.
- JDK 8 or later
- Contentstack account
- Latest version of IntelliJ IDEA / eclipse / vscode / spring tool suite
To setup Utils SDK in your Java project, add the following dependency in the pom.xml file
<dependency>
<groupId>com.contentstack.sdk</groupId>
<artifactId>util</artifactId>
<version>{latest}</version>
</dependency>If you are using Contentstack Java SDK, then the Utils SDK is already imported into your project, and the dependency snippet will look as below
<dependency>
<groupId>com.contentstack.sdk</groupId>
<artifactId>java</artifactId>
<version>{latest}</version>
</dependency>Download Latest dependency here
Let’s learn how you can use Utils SDK to render embedded items.
To render embedded items on the front-end, use the renderContents function, and define the UI elements you want to show in the front-end of your website, as shown in the example code below:
packagecom.contentstack.utils;
importcom.contentstack.utils.helper.Metadata;
importcom.contentstack.utils.interfaces.NodeCallback;
importcom.contentstack.utils.node.MarkType;
importcom.contentstack.utils.render.DefaultOption;
importorg.json.JSONObject;
publicclassDefaultOptionClassextendsDefaultOption {
@OverridepublicStringrenderOptions(JSONObjectembeddedObject, Metadatametadata) {
switch (metadata.getStyleType()) {
caseBLOCK:
return"<p>" + embeddedObject.getString("title") + "</p><span>" +
embeddedObject.getString("multi") + "</span>";
caseINLINE:
return"<p>" + embeddedObject.getString("title") + "</p><span>" +
embeddedObject.getString("line") + "</span>";
caseLINK:
return"<p>" + embeddedObject.getString("title") + "</p><span>" +
embeddedObject.getString("key") + "</span>";
caseDISPLAY:
return"<p>" + embeddedObject.getString("someTitle") + "</p><span>" +
embeddedObject.getString("multi") + "</span>";
default:
returnsuper.renderOptions(embeddedObject, metadata);
}
}
@OverridepublicStringrenderMark(MarkTypemarkType, StringrenderText) {
if (markType == MarkType.BOLD) {
return"<b>" + renderText + "</b>";
}
returnsuper.renderMark(markType, renderText);
}
@OverridepublicStringrenderNode(StringnodeType, JSONObjectnodeObject, NodeCallbackcallback) {
if (nodeType.equalsIgnoreCase("paragraph")) {
Stringchildren = callback.renderChildren(nodeObject.optJSONArray("children"));
return"<p class='class-id'>" + children + "</p>";
}
returnsuper.renderNode(nodeType, nodeObject, callback);
}
}
Contentstack Utils SDK lets you interact with the Content Delivery APIs and retrieve embedded items from the RTE field of an entry.
To get an embedded item of a single entry, you need to provide the stack API key, environment name, delivery token, content type’s UID and entry’s UID. Then, use the includeEmbeddedItems function as shown below:
importContentstackStackstack=Contentstack.stack("apiKey","deliveryToken","environment_name");
ContentTypecontentType=stack.contentType("content_type_uid");
Entryentry=contentType.entry("entry_uid");
entry.includeEmbeddedItems();
entry.fetch(newEntryResultCallBack(){
@OverridepublicvoidonCompletion(ResponseTyperesponseType,Errorerror){
if(error==null){
// [Success block]String[]keyPath={
"rich_text_editor","global_rich_multiple.group.rich_text_editor"
};
JSONObjectjsonObject=entry.toJSON();
Utils.render(jsonObject,keyPath,newOption());
}else{
[Errorblock] // handle your error
}}
});To get embedded items from multiple entries, you need to provide the stack API key, environment name, delivery token, and content type’s UID.
importContentstackStackstack=Contentstack.stack("apiKey","deliveryToken","environment_name");
Queryquery=stack.contentType("content_type_uid").query();
query.includeEmbeddedItems();
query.find(newQueryResultsCallBack(){
@OverridepublicvoidonCompletion(ResponseTyperesponseType,QueryResultqueryResult,Errorerror){
if(error==null){
List<Entry> entries=queryresult.getResultObjects();
String[]keyPath={
"rich_text_editor","global_rich_multiple.group.rich_text_editor"
};
for(Entryentry:entries){
JSONObjectjsonObject=entry.toJSON();
Utils.render(jsonObject,keyPath,newOption());
}
}else{
[Errorblock] // handle your error
}}
});To get multiple entries, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use the Utils.jsonToHTML function as shown below:
importContentstackStackstack=Contentstack.stack("apiKey","deliveryToken","environment_name");
Queryquery=stack.contentType("content_type_uid").query();
query.includeEmbeddedItems(); // include embedded itemsquery.find(newQueryResultsCallBack(){
@OverridepublicvoidonCompletion(ResponseTyperesponseType,QueryResultqueryResult,Errorerror){
if(error==null){
List<Entry> entries=queryresult.getResultObjects();
String[]keyPath={
"rte_fieldUid","group.rteFieldUID"
};
for(Entryentry:entries){
JSONObjectjsonObject=entry.toJSON();
Utils.jsonToHTML(jsonObject,keyPath,newOption());
}
}}
});