Advanced Type Retrieval

This is a simple how to on different aspects of the Contentful JAVA API. All following samples assume a client setup like

1CDAClient client = CDAClient.builder()
2 .setSpace("developer_bookshelf")
3 .setToken("0b7f6x59a0")
4 .build();

as described in our getting started guide.

All entries

To fetch all entries of the given space, you could use:

1CDAArray array = client.fetch(CDAEntry.class).all();

This will result in

1{
2 "total": 2,
3 "skip": 0,
4 "limit": 100,
5 "items": [
6 {
7 "fields": {
8 "author": {
9 "en-US": "Larry Wall"
10 },
11 "name": {
12 "en-US": "An introduction to regular expressions. Volume VI"
13 },
14 "description": {
15 "en-US": "Now you have two problems."
16 }
17 },
18
19 },
20 … another item
21 ],
22 "sys": {
23 "type": "Array"
24 }
25}

One specific entry

Using the .one(<YOUR_ITEM_ID>) method of the client, like follows

1CDAEntry entry = client.fetch(CDAEntry.class).one("5PeGS2SoZGSa4GuiQsigQu");

you’ll get a response like

1{
2 "fields": {
3 "author": {
4 "en-US": "Larry Wall"
5 },
6 "name": {
7 "en-US": "An introduction to regular expressions. Volume VI"
8 },
9 "description": {
10 "en-US": "Now you have two problems."
11 }
12 },
13
14}

All assets

Retrieving all assets of a space, this snippet could help:

1CDAArray array = client.fetch(CDAAsset.class).all();

And this will be the result for a different (space = cfexampleapi, token = b4c0n73n7fu1) sample space…

1{
2 "total": 4,
3 "skip": 0,
4 "limit": 100,
5 "items": [
6 {
7 "fields": {
8 "file": {
9 "en-US": {
10 "fileName": "jake.png",
11 "contentType": "image/png",
12 "details": {
13 "image": {
14 "width": 100.0,
15 "height": 161.0
16 },
17 "size": 20480.0
18 },
19 "url": "//images.ctfassets.net/cfexampleapi/4hlteQAXS8iS0YCMU6QMWg/2a4d826144f014109364ccf5c891d2dd/jake.png"
20 }
21 },
22 "title": {
23 "en-US": "Jake"
24 }
25 },
26 "sys": {
27 "type": "Asset",
28 "id": "jake",
29
30 }
31
32 },
33
34 ],
35
36}

Only one asset

Retrieving one specific asset, take a look at this example:

1CDAAsset asset = client.fetch(CDAAsset.class).one("jake");

And this will be the result in only one asset (again using the cfexampleapi space):

1{
2 "fields": {
3 "file": {
4 "en-US": {
5 "fileName": "jake.png",
6 "contentType": "image/png",
7 "details": {
8 "image": {
9 "width": 100.0,
10 "height": 161.0
11 },
12 "size": 20480.0
13 },
14 "url": "//images.ctfassets.net/cfexampleapi/4hlteQAXS8iS0YCMU6QMWg/2a4d826144f014109364ccf5c891d2dd/jake.png"
15 }
16 },
17 "title": {
18 "en-US": "Jake"
19 }
20 },
21 "sys": {
22 "type": "Asset",
23 "id": "jake",
24
25 }
26
27}

Content types

In order to request all content types, you could use a call like this:

1CDAArray array = client.fetch(CDAContentType.class).all();

Taking the resulting object, it’ll be printed as (This example will only contain one content type, since the space only contains one type).

1{
2 "total": 1,
3 "skip": 0,
4 "limit": 100,
5 "items": [
6 {
7 "fields": [
8 {
9 "name": "Name",
10 "id": "name",
11 "type": "Symbol",
12 "disabled": false,
13 "required": false,
14 "localized": false
15 },
16 {
17 "name": "Author",
18 "id": "author",
19 "type": "Symbol",
20 "disabled": false,
21 "required": false,
22 "localized": false
23 },
24 {
25 "name": "Description",
26 "id": "description",
27 "type": "Symbol",
28 "disabled": false,
29 "required": false,
30 "localized": false
31 }
32 ],
33 "name": "Book",
34 "displayField": "name",
35 "description": "",
36
37 }
38 ],
39 "assets": {},
40 "entries": {},
41 "sys": {
42 "type": "Array"
43 }
44}

Fetching details of the current space

To fetch the current space meta information, you could use

1CDASpace space = client.fetchSpace();

yielding

1{
2 "name": "Developer Bookshelf",
3 "locales": [
4 {
5 "code": "en-US",
6 "name": "U.S. English",
7 "default": true
8 }
9 ],
10 "defaultLocale": {
11 "code": "en-US",
12 "name": "U.S. English",
13 "default": true
14 },
15 "sys": {
16 "type": "Space",
17 "id": "developer_bookshelf"
18 }
19}

Fetching all spaces

For this example, you will need to get a CMA API token and create a CMAClient using it:

1CMAClient cmaClient = new CMAClient.Builder()
2 .setAccessToken(CMA_TOKEN)
3 .build();
4
5CMAArray<CMASpace> spaces = cmaClient.spaces().fetchAll();
6text = gson.toJson(spaces);
7System.out.println(text);

And the result will be:

1{
2 "items": [
3 {
4 "name": "Your Space here!",
5 "sys": {
6 "createdAt": "",
7 "updatedBy": {
8
9 },
10 "createdBy": {
11
12 },
13 "id": "",
14 "type": "Space",
15 "version": 2.0,
16 "updatedAt": ""
17 }
18 },
19 … (More spaces, as much as you own)
20 ],
21 "total":,
22 "skip": 0,
23 "limit": 25,
24 "sys": {
25 "type": "Array"
26 }
27}

Next steps