Class JsonApiDocument
Carries the standard six top-level members: data, errors, meta, jsonapi,
links, included.
Mutual exclusion
Per spec, a document MUST contain at least one of data/errors/meta, and data
and errors MUST NOT coexist. The validate() helper checks both rules and throws
IllegalStateException on violation.
The polymorphic data member
The top-level data can be null, a single JsonApiResource, or an array of them. We
model data as a plain Object property; the writer chooses single-vs-array shape based on
which value was set. Typed setters setData(JsonApiResource) and setData(List) are provided
for convenience.
See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddErrors(Collection<JsonApiError> value) Bean property appender:errors .addErrors(JsonApiError... value) Bean property appender:errors .addIncluded(Collection<JsonApiResource> value) Bean property appender:included .addIncluded(JsonApiResource... value) Bean property appender:included .getData()Bean property getter:data .Bean property getter:errors .Bean property getter:included .Bean property getter:jsonapi .getLinks()Bean property getter:links .getMeta()Bean property getter:meta .Convenience method to add a single link.Convenience method to add a single meta entry.Bean property setter:data .setData(List<JsonApiResource> value) Typed setter for to-many primary data.setData(JsonApiResource value) Typed setter for single-resource primary data.setErrors(List<JsonApiError> value) Bean property setter:errors .setErrors(JsonApiError... value) Bean property setter:errors .setIncluded(List<JsonApiResource> value) Bean property setter:included .setIncluded(JsonApiResource... value) Bean property setter:included .setJsonapi(JsonApiVersion value) Bean property setter:jsonapi .Bean property setter:links .Bean property setter:meta .toString()validate()Validates document-level invariants per the JSON:API spec.
-
Constructor Details
-
JsonApiDocument
public JsonApiDocument()Default constructor.
-
-
Method Details
-
getData
Bean property getter:data .Top-level resource linkage. May be
null, a singleJsonApiResource, or aList<JsonApiResource>.- Returns:
- The value of the
data property, ornull if it is not set.
-
setData
Bean property setter:data .- Parameters:
value- The new value (single resource, list of resources, ornullto unset).- Returns:
- This object.
-
setData
Typed setter for single-resource primary data.- Parameters:
value- The resource. Can benull to unset the property.- Returns:
- This object.
-
setData
Typed setter for to-many primary data.- Parameters:
value- The list of resources. Can benull to unset the property.- Returns:
- This object.
-
getErrors
Bean property getter:errors .- Returns:
- The value of the
errors property, ornull if it is not set.
-
setErrors
Bean property setter:errors .- Parameters:
value- The new value for this property. Can benull to unset the property.- Returns:
- This object.
-
setErrors
Bean property setter:errors .- Parameters:
value- The new value for this property.- Returns:
- This object.
-
addErrors
Bean property appender:errors .- Parameters:
value- The error objects to append.- Returns:
- This object.
-
addErrors
Bean property appender:errors .- Parameters:
value- The error objects to append.- Returns:
- This object.
-
getMeta
Bean property getter:meta .- Returns:
- The value of the
meta property, ornull if it is not set.
-
setMeta
Bean property setter:meta .- Parameters:
value- The new value for this property. Can benull to unset the property.- Returns:
- This object.
-
putMeta
Convenience method to add a single meta entry.- Parameters:
name- The meta key. Can benull (LinkedHashMaptolerates anull key).value- The meta value. Can benull (stored asnull ).- Returns:
- This object.
-
getJsonapi
Bean property getter:jsonapi .- Returns:
- The value of the
jsonapi property, ornull if it is not set.
-
setJsonapi
Bean property setter:jsonapi .- Parameters:
value- The new value for this property. Can benull to unset the property.- Returns:
- This object.
-
getLinks
Bean property getter:links .Per spec, each
linksvalue can be either a JSON string URL or aJsonApiLinkobject.- Returns:
- The value of the
links property, ornull if it is not set.
-
setLinks
@BeanProp("links") @Swap(JsonApiLinkOrStringSwap.class) public JsonApiDocument setLinks(Map<String, Object> value) Bean property setter:links .- Parameters:
value- The new value for this property. Can benull to unset the property.- Returns:
- This object.
-
putLink
Convenience method to add a single link.- Parameters:
name- The link name. Can benull (LinkedHashMaptolerates anull key).value- The link value (aStringURL or aJsonApiLinkobject). Can benull (stored asnull ).- Returns:
- This object.
-
getIncluded
Bean property getter:included .- Returns:
- The value of the
included property, ornull if it is not set.
-
setIncluded
Bean property setter:included .- Parameters:
value- The new value for this property. Can benull to unset the property.- Returns:
- This object.
-
setIncluded
Bean property setter:included .- Parameters:
value- The new value for this property.- Returns:
- This object.
-
addIncluded
Bean property appender:included .- Parameters:
value- The resource objects to append.- Returns:
- This object.
-
addIncluded
Bean property appender:included .- Parameters:
value- The resource objects to append.- Returns:
- This object.
-
validate
Validates document-level invariants per the JSON:API spec.Throws if
dataanderrorsare both present, or if none ofdata/errors/metais present.- Returns:
- This object.
- Throws:
IllegalStateException- If the document violates a top-level invariant.
-
toString
-