Java SDK for Content Management API. It helps in editing and creating content stored in Contentful with Java applications.
Contentful provides a content infrastructure for digital teams to power content in websites, apps, and devices. Contentful, unlike any other CMS, is built to integrate with the modern software stack. It offers a central hub for structured content, powerful management and delivery APIs, and a customizable web app that enable developers and content creators to ship digital products faster.
Table of contents
- Content manipulation through Content Management API.
- Supported Endpoints
Install the Contentful dependency:
- Maven
<dependency>
<groupId>com.contentful.java</groupId>
<artifactId>cma-sdk</artifactId>
<version>3.4.2</version>
</dependency>
- Gradle
compile 'com.contentful.java:cma-sdk:3.4.2'
This SDK requires Java 8 (or higher version).
The CMAClient
manages all interactions with the Content Management API.
final CMAClient client =
new CMAClient
.Builder()
.setAccessToken("<access_token>")
.build();
The Access Token can easily be obtained through the management API documentation.
By selecting a Module, say .entries()
and using its manipulator methods, like .fetchAll()
, changes can be applied to the underlying resources. Resources can be fetched as following:
final CMAArray<CMAEntry> array =
client
.entries()
.fetchAll();
A client performs various operations on different types of Resources (such as Assets, Content Types, Entries, Spaces, etc). Every type of Resource is represented by a Module in the CMAClient
class, for example:
client.spaces() // returns the Spaces Module
client.entries() // returns the Entries Module
client.assets() // returns the Assets Module
…
Each Module contains a set of methods that perform various operations on the specified Resource type. Every method has a corresponding asynchronous extension which can be accessed through the async()
method of the Module, for example the following synchronous call
final CMAArray<CMASpace> array =
client
.spaces()
.fetchAll(
"space_id",
"environment_id"
);
can be expressed by this asynchronous call:
final CMAArray<CMASpace> array =
client
.spaces()
.async()
.fetchAll(
"space_id",
"environment_id",
new CMACallback<CMAArray<CMASpace>>() {
@Override protected void onSuccess(CMAArray<CMASpace> result) {
// success
}
@Override protected void onFailure(RetrofitError retrofitError) {
// failure
}
});
Note: The default
CMACallback
has an emptyonFailure()
implementation. If failures are of interest, overriding this method is mandatory.
Note: The CMA documentation offers more code snippets for all Modules.
Instead of repeating the Space and _Environment _ids with every call like so
final CMAArray<CMAEntry> array =
client
.entries()
.fetchAll(
"space_id",
"environment_id",
);
the client can be configured to always use the same values:
final CMAClient client =
new CMAClient
.Builder()
.setAccessToken("<access_token>")
.setSpaceId("<space_id>")
.setEnvironmentId("<environment_id>")
.build();
This changes the parameters Modules are using:
final CMAArray<CMAEntry> array =
client
.entries()
.fetchAll();
Words of warning
The Modules apiKeys
, environments
, roles
, spaceMemberships
, uiExtensions
, uploads
, and webhooks
, do not support Environments different to master
. If the above configuration is used with these Modules, they throw an exception
. Creation of a new client without specifying an Environment id is needed:
final CMAArray<CMAApiKey> array =
client
.apiKeys()
.fetchAll(
"spaceid"
);
The SDK uses Retrofit as a REST client, which detects OkHttp in the classpath and uses it if available, or else it falls back to the default HttpURLConnection
.
The recommended approach would be to add OkHttp as a dependency to your project, but that is completely optional.
It is possible to specify a custom client to be used, refer to the official documentation for instructions.
In order to create and update rich text fields, the following rule set is enforced from Contentful:
- A Document cannot be nested inside a Document.
- A Document can only contain Paragraphs, Lists, or Texts.
- Paragraphs can contain Paragraphs or Text and Link nodes.
- Links have to be inside of Paragraphs.
- A List has to be inside of a Document.
- A ListItem has to contain at least one Paragraph.
For supported and tested combinations of Nodes, please take a look at the Rich Text End to End tests, those will be kept updated over time and should give an overview on what is possible.
Snapshots of the development version are available through
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
compile 'com.contentful.java:cma-sdk:3.3.3-SNAPSHOT'
maven { url 'https://jitpack.io' }
compile 'com.github.contentful:contentful.java:cma-sdk-3.3.3-SNAPSHOT'
See
Copyright (C) 2019 Contentful GmbH. See LICENSE.txt for further details.
Contentful wants to provide a safe, inclusive, welcoming, and harassment-free space and experience for all participants, regardless of gender identity and expression, sexual orientation, disability, physical appearance, socioeconomic status, body size, ethnicity, nationality, level of experience, age, religion (or lack thereof), or other identity markers.