Class JsonApiModelBuilder
This builder provides a type-safe, idiomatic way to construct JSON:API documents including:
- Primary data (single resources or collections)
- Relationships between resources
- Included resources for compound documents
- Links at document and resource levels
- Meta information
- Sparse fieldsets for attribute filtering
- Pagination metadata
The builder ensures compliance with the JSON:API specification and integrates seamlessly with Spring HATEOAS representation models.
Example usage:
JsonApiModel jsonApiModel = JsonApiModelBuilder.jsonApiModel()
.model(EntityModel.of(movie))
.relationship("directors", directors)
.included(directors)
.build();
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.hateoas.RepresentationModel<?> build()Transform the entities, Links, relationships and included into aRepresentationModel.Adds a sparse fieldset for the given JSON:API type.Adds the givenObjectto the includedRepresentationModels.included(Collection<?> collection) Adds the givenCollectionto the includedRepresentationModels.included(org.springframework.hateoas.RepresentationModel<?> representationModel) Adds the givenRepresentationModelto the includedRepresentationModels.static JsonApiModelBuilderCreates a newJsonApiModelBuilder.Adds aLinkwith the given href andLinkRelationto the top level of the JSON:API document.link(org.springframework.hateoas.Link link) Adds aLinkto the top level of the JSON:API document.Adds multipleLinks to the top level of the JSON:API document.Adds the given key/value pair to the JSON:API meta.Creates anEntityModelfrom the given object as the primary data for the JSON:API document.model(org.springframework.hateoas.RepresentationModel<?> model) Sets theRepresentationModelas the primary data for the JSON:API document to be built.Creates all pagination links with JSON:API default request parameters for page number page[number] and page size page[size].Creates all pagination links.pageMeta()Adds the paging information to the JSON:API meta.relationship(String name, @Nullable Object dataObject) Adds or updates a relationship based on theObject.relationship(String name, @Nullable String selfLink, @Nullable String relatedLink, @Nullable org.springframework.hateoas.Links otherLinks) Adds or updates a relationship based on the links to theRepresentationModelto be built.Adds or updates a relationship based on the meta to theRepresentationModelto be built.relationship(String name, Collection<?> collection) Adds or updates a relationship based on theCollection.relationship(String name, Map<String, Object> meta) Adds or updates a relationship based on the meta to theRepresentationModelto be built.relationship(String name, @Nullable org.springframework.hateoas.EntityModel<?> entityModel, @Nullable String selfLink, @Nullable String relatedLink) Adds or updates a relationship based on the givenEntityModeland links.relationship(String name, org.springframework.hateoas.EntityModel<?> entityModel) Adds or updates a relationship based on the givenEntityModelto theRepresentationModelto be built.If called (anywhere in the builder sequence), the data portion of this relationship will always be rendered as an array, even if the data is not set or is one single element, e.g.Adds or updates a relationship with explicit empty array data.Adds or updates a relationship with explicit null data.
-
Method Details
-
model
Sets theRepresentationModelas the primary data for the JSON:API document to be built.The model can be an
EntityModel,CollectionModel, orPagedModel. This forms the main content of the JSON:APIdatafield.Note: If a model is already set, an
IllegalStateExceptionwill be thrown to prevent accidental overwriting.- Parameters:
model- the representation model to use as primary data; must not be null- Returns:
- this builder instance for method chaining; will never be null
- Throws:
IllegalArgumentException- ifmodelis nullIllegalStateException- if a model has already been set on this builder
-
model
Creates anEntityModelfrom the given object as the primary data for the JSON:API document.This is a convenience method that automatically wraps the object in an
EntityModelbefore setting it as the model. Equivalent to callingmodel(EntityModel.of(object)).Note: If a model is already set, an
IllegalStateExceptionwill be thrown.- Parameters:
object- the object to wrap in anEntityModel; must not be null- Returns:
- this builder instance for method chaining; will never be null
- Throws:
IllegalArgumentException- ifobjectis nullIllegalStateException- if a model has already been set on this builder
-
link
Adds aLinkto the top level of the JSON:API document.This link will be rendered in the document-level
linksobject, not inside the resource object itself. If you need to add a link to the resource, add it directly to the model passed tomodel(RepresentationModel).- Parameters:
link- the link to add; must not be null- Returns:
- this builder instance for method chaining; will never be null
- Throws:
IllegalArgumentException- iflinkis null
-
link
Adds aLinkwith the given href andLinkRelationto the top level of the JSON:API document.This is a convenience method equivalent to
link(Link.of(href, relation)).- Parameters:
href- the link href; must not be nullrelation- the link relation; must not be null- Returns:
- this builder instance for method chaining; will never be null
- Throws:
IllegalArgumentException- ifhreforrelationis null
-
links
Adds multipleLinks to the top level of the JSON:API document.- Parameters:
links- the links to add; must not be null- Returns:
- this builder instance for method chaining; will never be null
- Throws:
IllegalArgumentException- iflinksis null
-
relationship
Adds or updates a relationship based on theObject. It must be possible to extract the JSON:API id of this object, see reference doc. If there is already a relationship for the given name defined, the new data object will be added to the existing relationship. If the dataObject is null, an empty to-one relationship is created, see JSON:API doc.- Parameters:
name- must not be null.dataObject- must not be null.- Returns:
- will never be null.
-
relationship
Adds or updates a relationship based on theCollection. It must be possible to extract the JSON:API id of all elements of this collection, see reference doc. If there is already a relationship for the given name defined, the elements of the collection will be added to the existing relationship.- Parameters:
name- must not be null.collection- must not be null.- Returns:
- will never be null.
-
relationship
public JsonApiModelBuilder relationship(String name, org.springframework.hateoas.EntityModel<?> entityModel) Adds or updates a relationship based on the givenEntityModelto theRepresentationModelto be built. If there is already a relationship for the given name defined, the newEntityModelwill be added to the existing relationship.- Parameters:
name- must not be null.entityModel- must not be null.- Returns:
- will never be null.
-
relationship
public JsonApiModelBuilder relationship(String name, @Nullable org.springframework.hateoas.EntityModel<?> entityModel, @Nullable String selfLink, @Nullable String relatedLink) Adds or updates a relationship based on the givenEntityModeland links. A self link of the relation and a related link (to the related resource) can also be added. While entityModel, selfLink, and relatedLink can be null, at least one of them has to be not null.- Parameters:
name- must not be null.entityModel- can be null.selfLink- can be null.relatedLink- can be null.- Returns:
- will never be null.
-
relationship
Adds or updates a relationship based on the meta to theRepresentationModelto be built. If there is already a relationship for the given name defined, the meta will overwrite the existing relationship.- Parameters:
name- must not be null.meta- must not be null.- Returns:
- will never be null.
-
relationship
public JsonApiModelBuilder relationship(String name, Object dataObject, Map<String, Object> resourceIdentifierMeta) Adds or updates a relationship based on the meta to theRepresentationModelto be built. If there is already a relationship for the given name defined, the meta will overwrite the existing relationship.- Parameters:
name- must not be null.dataObject- must not be null.resourceIdentifierMeta- can be null.- Returns:
- will never be null.
-
relationship
public JsonApiModelBuilder relationship(String name, @Nullable String selfLink, @Nullable String relatedLink, @Nullable org.springframework.hateoas.Links otherLinks) Adds or updates a relationship based on the links to theRepresentationModelto be built. If there is already a relationship for the given name defined, the new links will overwrite the existing ones.- Parameters:
name- must not be null.selfLink- can be null.relatedLink- can be null.otherLinks- can be null.- Returns:
- will never be null.
-
relationshipWithDataArray
If called (anywhere in the builder sequence), the data portion of this relationship will always be rendered as an array, even if the data is not set or is one single element, e.g. "data": [] or "data" : [{"id":"1", "type":"movies"}]. This is convenient if the consumer always expects a (one to many) relationship to be rendered as an array rather than having to check for null values or single objects.- Parameters:
name- must not be null.- Returns:
- will never be null.
-
relationshipWithNullData
Adds or updates a relationship with explicit null data. This will be serialized as "data": null in JSON:API format, representing an empty to-one relationship as per the JSON:API specification. If there is already a relationship for the given name defined, the data will be replaced with null while preserving links and meta.- Parameters:
name- must not be null.- Returns:
- will never be null.
-
relationshipWithEmptyData
Adds or updates a relationship with explicit empty array data. This will be serialized as "data": [] in JSON:API format, representing an empty to-many relationship as per the JSON:API specification. If there is already a relationship for the given name defined, the data will be replaced with an empty array while preserving links and meta.- Parameters:
name- must not be null.- Returns:
- will never be null.
-
included
public JsonApiModelBuilder included(org.springframework.hateoas.RepresentationModel<?> representationModel) Adds the givenRepresentationModelto the includedRepresentationModels. It will appear then top level in the JSON:API included entities. Duplicates with same id and type will be eliminated.- Parameters:
representationModel- must not be null.- Returns:
- will never be null.
-
included
Adds the givenObjectto the includedRepresentationModels. The object is automatically wrapped into anEntityModel. It will appear then top level in the JSON:API included entities. Duplicates with same id and type will be eliminated.- Parameters:
object- must not be null.- Returns:
- will never be null.
-
included
Adds the givenCollectionto the includedRepresentationModels. The objects of the collection are automatically wrapped into anEntityModel, if they are not alreadyRepresentationModels. The members of the collection will appear then top level in the JSON:API included entities. Duplicates with same id and type will be eliminated.- Parameters:
collection- must not be null.- Returns:
- will never be null.
-
meta
Adds the given key/value pair to the JSON:API meta.- Parameters:
key- the json keyvalue- the json value- Returns:
- will never be null.
-
pageMeta
Adds the paging information to the JSON:API meta. Preconditions are: - the model has been added before - the model is a paged model - the model contains a Pageable- Returns:
- will never be null.
-
pageLinks
Creates all pagination links with JSON:API default request parameters for page number page[number] and page size page[size].Preconditions are:
- the model has been added before
- the model is a PagedModel
- the model contains PageMetadata
- Parameters:
linkBase- the prefix of all pagination links, e.g. the base URL of the collection resource- Returns:
- will never be null.
-
pageLinks
public JsonApiModelBuilder pageLinks(String linkBase, String pageNumberRequestParam, String pageSizeRequestParam) Creates all pagination links.Preconditions are:
- the model has been added before
- the model is a PagedModel
- the model contains PageMetadata
- Parameters:
linkBase- the prefix of all pagination links, e.g. the base URL of the collection resourcepageNumberRequestParam- the request parameter for page numberpageSizeRequestParam- the request parameter for page size- Returns:
- will never be null.
-
fields
Adds a sparse fieldset for the given JSON:API type. Only the resource objects attributes that are in the fields parameters will be serialized to JSON. THis will apply to data attributes and attributes of included resources. This will not exclude relationships, if the name of a relationship for the given JSON:API type is not part of the fields parameters.- Parameters:
jsonapiType- the JSON:API typefields- the attributes that should be included- Returns:
- ill never be null.
-
build
public org.springframework.hateoas.RepresentationModel<?> build()Transform the entities, Links, relationships and included into aRepresentationModel.- Returns:
- will never be null.
-
jsonApiModel
Creates a newJsonApiModelBuilder.- Returns:
- will never be null.
-