Class JsonApiModelBuilder
java.lang.Object
com.toedter.spring.hateoas.jsonapi.JsonApiModelBuilder
Builder API to create complex JSON:API representations exposing a JSON:API idiomatic API.
It includes building JSON:API relationships and included.
-
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 givenObject
to the includedRepresentationModel
s.included
(Collection<?> collection) Adds the givenCollection
to the includedRepresentationModel
s.included
(org.springframework.hateoas.RepresentationModel<?> representationModel) Adds the givenRepresentationModel
to the includedRepresentationModel
s.static JsonApiModelBuilder
Creates a newJsonApiModelBuilder
.Adds aLink
with the given href andLinkRelation
to theRepresentationModel
to be built.link
(org.springframework.hateoas.Link link) Adds aLink
to theRepresentationModel
to be built.Adds the givenLink
s to theRepresentationModel
to be built.Adds the given key/value pair to the JSON:API meta.Creates anEntityModel
from the object as the base for the RepresentationModel to be built.model
(org.springframework.hateoas.RepresentationModel<?> model) Sets theRepresentationModel
as the base for the RepresentationModel 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, Object dataObject) Adds or updates a relationship based on theObject
.Adds or updates a relationship based on the meta to theRepresentationModel
to be built.relationship
(String name, String selfLink, String relatedLink, org.springframework.hateoas.Links otherLinks) Adds or updates a relationship based on the links to theRepresentationModel
to 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 theRepresentationModel
to be built.relationship
(String name, org.springframework.hateoas.EntityModel<?> entityModel) Adds or updates a relationship based on the givenEntityModel
to theRepresentationModel
to be built.relationship
(String name, org.springframework.hateoas.EntityModel<?> entityModel, String selfLink, String relatedLink) Adds or updates a relationship based on the givenEntityModel
and links.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.
-
Method Details
-
model
Sets theRepresentationModel
as the base for the RepresentationModel to be built.NOTE: If the model is already set, an IllegalStateException will be thrown.
- Parameters:
model
- must not be null.- Returns:
- will never be null.
-
model
Creates anEntityModel
from the object as the base for the RepresentationModel to be built.NOTE: If the model is already set, an IllegalStateException will be thrown.
- Parameters:
object
- must not be null.- Returns:
- will never be null.
-
link
Adds aLink
to theRepresentationModel
to be built.NOTE: This adds it to the top level. If you need a link inside the model you added with
model(RepresentationModel)
method, add it directly to the model.- Parameters:
link
- must not be null.- Returns:
- will never be null.
-
link
Adds aLink
with the given href andLinkRelation
to theRepresentationModel
to be built.- Parameters:
href
- must not be null.relation
- must not be null.- Returns:
- will never be null.
-
links
Adds the givenLink
s to theRepresentationModel
to be built.- Parameters:
links
- must not be null.- Returns:
- will never be 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 givenEntityModel
to theRepresentationModel
to be built. If there is already a relationship for the given name defined, the newEntityModel
will 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 givenEntityModel
and 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 theRepresentationModel
to 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 theRepresentationModel
to 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 theRepresentationModel
to 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.
-
included
public JsonApiModelBuilder included(org.springframework.hateoas.RepresentationModel<?> representationModel) Adds the givenRepresentationModel
to the includedRepresentationModel
s. 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 givenObject
to the includedRepresentationModel
s. 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 givenCollection
to the includedRepresentationModel
s. The objects of the collection are automatically wrapped into anEntityModel
, if they are not alreadyRepresentationModel
s. 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.
-