Skip to main content

Headless

In a headless Brightspot implementation, the view system delivers content via a JSON API to a consuming application; that application processes and transforms the content using its own logic. In headless Brightspot implementations, two scenarios are common:

  • Delivering APIs to mobile applications
  • Delivering APIs to JavaScript frameworks (React, Angular, or others)

Producing your JSON API requires two steps:

  1. Create a model
  2. Create a view model

The following sections provide examples for each of those steps.

Model

The following snippet is a basic model for an article.

1
import com.psddev.cms.db.Content;
2
import com.psddev.dari.db.Recordable;
3
4
public class Article extends Content {
5
6
private String headline;
7
8
private String body;
9
10
public String getHeadline() {
11
return headline;
12
}
13
14
public void setHeadline(String headline) {
15
this.headline = headline;
16
}
17
18
public String getBody() {
19
return body;
20
}
21
22
public void setBody(String body) {
23
this.body = body;
24
}
25
}

View Model

The following snippet is a headless view model that serializes a model’s data into JSON format.

1
import com.psddev.cms.view.JsonView;
2
import com.psddev.cms.view.PageEntryView;
3
import com.psddev.cms.view.ViewInterface;
4
import com.psddev.cms.view.ViewModel;
5
6
@JsonView
7
@ViewInterface
8
public class ArticleViewModel extends ViewModel<Article> implements PageEntryView {
9
10
public String getBody() {
11
return model.getBody();
12
}
13
14
public String getHeadline() {
15
return model.getHeadline();
16
}
17
}
  • Indicates output format for this view model is a JSON representation.
  • Indicates the source model is Article.

At run time, Brightspot manages the interaction between the model and view model to produce the JSON file containing field names and associated values.

1
{
2
"headline" : "JSON Example",
3
"body" : "Brightspot automatically produces JSON from the view model."
4
}

The consuming application has the responsibility of processing the JSON output for downstream purposes.