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:
- Create a model
- 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.
1import com.psddev.cms.db.Content;2import com.psddev.dari.db.Recordable;34public class Article extends Content {56private String headline;78private String body;910public String getHeadline() {11return headline;12}1314public void setHeadline(String headline) {15this.headline = headline;16}1718public String getBody() {19return body;20}2122public void setBody(String body) {23this.body = body;24}25}
View Model
The following snippet is a headless view model that serializes a model’s data into JSON format.
1import com.psddev.cms.view.JsonView;2import com.psddev.cms.view.PageEntryView;3import com.psddev.cms.view.ViewInterface;4import com.psddev.cms.view.ViewModel;56@JsonView7@ViewInterface8public class ArticleViewModel extends ViewModel<Article> implements PageEntryView {910public String getBody() {11return model.getBody();12}1314public String getHeadline() {15return model.getHeadline();16}17}
- 6. Indicates output format for this view model is a JSON representation.
- 8. 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.