I have this abstract class that provides some common properties to my entities. The following is an excerpt:
@MappedSuperclass
public class AbstractEntity implements Serializable {
@Id
@GeneratedValue
private long id;
@Temporal(value = TemporalType.TIMESTAMP)
@JsonProperty(access = Access.READ_ONLY)
private Date createdOn;
@Temporal(value = TemporalType.TIMESTAMP)
@JsonProperty(access = Access.READ_ONLY)
private Date modifiedOn;
⋮
}
When serializing a subclass to JSON I get the expected results, for instance, this is an excerpt from serialization:
{
"createdOn": "2016-12-11T15:35:23Z",
"modifiedOn": "2016-12-11T15:35:23Z",
⋮
}
I need to have those common properties serialized to a JSON object such that the above example looks like this:
{
"_metadata": {
"createdOn": "2016-12-11T15:35:23Z",
"modifiedOn": "2016-12-11T15:35:23Z",
}
⋮
}
I have already tried using a class called Metadata
and having a property of that type does works well. But I'm wondering if there's an easier or simpler way just using Jackson annotations?