I'm trying to workout how I should be structuring my JSON objects to best comply with http://jsonapi.org/format/
If I used the following classes:
Page (main object)
public class Page {
@Id
@ObjectId
private String id; //Used for mongodb
@JsonProperty
private String type;
@JsonProperty
private Attribute attributes;
@JsonProperty
private Meta meta;
public Page() {
// Jackson deserialization
}
// Getters and setters
}
Attributes (nested into page)
public class Attribute {
@JsonProperty
private Date created = new Date();
@JsonProperty
private String title;
@JsonProperty
private String body;
public Attribute() {
// Jackson deserialization
}
public Attribute(Date created, String title, String body) {
this.created = created;
this.title = title;
this.body = body;
}
// Getters and setters
}
Meta (nested into page)
public class Meta {
@JsonProperty
private List<String> authors;
public Meta() {
}
public Meta(List<String> authors) {
this.authors = authors;
}
// Getters and setters
}
I can create this object with a post such as:
{
"type": "page",
"attributes": {
"title": "This is the title",
"body": "<p>This is a long section of html, other stuff</p>"
},
"meta": {
"authors": [
"Steve",
"John",
"Sam"
]
}
}
And the resulting JSON object is created:
{
id:"56cbed5036f66b05dc2df841",
type:"page",
attributes:{
created:1456205138886,
title:"This is the title",
body:"<p>This is a long section of html, other stuff</p>"
},
meta:{
authors:[
"Steve",
"John",
"Sam"
]
}
}
The question(s): Is creating multiple classes the way I have the optimal/correct way of creating nested JSON objects, and should I be trying to wrap this all inside "data:" as per the link above states is a MUST do? If that is the case should I create a single POJO called Data which contains the Page object?
When looking for information around this type of thing all I seem to be able to find is people asking how to deserialize JSON into POJOs, which isn't what I'm looking for.
Really want to find some best practises here for writing APIs.