Class HalModelBuilder
java.lang.Object
org.springframework.hateoas.mediatype.hal.HalModelBuilder
Builder API to create complex HAL representations exposing a HAL idiomatic API. It's built around the notion of a the
representation consisting of an optional primary entity and e set of embeds. There's also explicit API for common HAL
patterns like previews contained in _embedded for links present in the representation.
- Since:
- 1.1
- Author:
- Greg Turnquist, Oliver Drotbohm
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescription<T extends RepresentationModel<T>>
RepresentationModel<T>build()
Transform the entities andLink
s into aRepresentationModel
.Embeds the given entity into theRepresentationModel
.embed
(Object entity, LinkRelation linkRelation) Embed the entity and associate it with theLinkRelation
.embed
(Collection<?> collection) Embeds the given collection in theRepresentationModel
.embed
(Collection<?> collection, Class<?> type) Embeds the given collection in theRepresentationModel
and theLinkRelation
derived from the given type.embed
(Collection<?> collection, LinkRelation relation) Embeds the given collection in theRepresentationModel
for the givenLinkRelation
.Embeds the givenStream
in theRepresentationModel
.Embeds the givenStream
in theRepresentationModel
and theLinkRelation
derived from the given type.embed
(Stream<?> stream, LinkRelation relation) static HalModelBuilder
Creates a newHalModelBuilder
without a primary payload.Embed the entity, but with no relation.static HalModelBuilder
halModel()
Creates a newHalModelBuilder
.static HalModelBuilder
halModel
(EmbeddedWrappers wrappers) Creates a newHalModelBuilder
using the givenEmbeddedWrappers
.static HalModelBuilder
halModelOf
(Object entity) Creates a newHalModelBuilder
with the given entity as primary payload.link
(String href, LinkRelation relation) Add aLink
to the whole thing.Adds the givenLink
s to theRepresentationModel
to be built.Initiates the setup of a preview given the current payload.preview
(Collection<?> collection) Starts a preview setup for the givenCollection
as preview.preview
(Collection<?> collection, Class<?> type) Starts a preview setup for the givenCollection
as preview falling back to the given type if theCollection
is empty.
-
Method Details
-
halModel
Creates a newHalModelBuilder
.- Returns:
- will never be null.
-
halModel
Creates a newHalModelBuilder
using the givenEmbeddedWrappers
.- Parameters:
wrappers
- must not be null.- Returns:
- will never be null.
-
halModelOf
Creates a newHalModelBuilder
with the given entity as primary payload.- Parameters:
entity
- must not be null.- Returns:
-
emptyHalModel
Creates a newHalModelBuilder
without a primary payload.- Returns:
-
entity
Embed the entity, but with no relation.- Parameters:
entity
-- Returns:
-
embed
Embed the entity and associate it with theLinkRelation
.- Parameters:
entity
- must not be null.linkRelation
- must not be null.- Returns:
- will never be null.
-
embed
Embeds the given entity into theRepresentationModel
.- Parameters:
entity
- must not be null.- Returns:
- will never be null.
-
embed
Embeds the given collection in theRepresentationModel
. If the collection is empty nothing will be added to theRepresentationModel
.- Parameters:
collection
- must not be null.- Returns:
- will never be null.
-
embed
Embeds the given collection in theRepresentationModel
and theLinkRelation
derived from the given type. If the collection is empty nothing will be added to theRepresentationModel
an empty embed for the derivedLinkRelation
will be added.- Parameters:
collection
- must not be null.type
- the type to derive theLinkRelation
to be used, must not be null.- Returns:
- will never be null.
- See Also:
-
embed
Embeds the given collection in theRepresentationModel
for the givenLinkRelation
. If the collection is empty nothing will be added to theRepresentationModel
an empty embed for the derivedLinkRelation
will be added.- Parameters:
collection
- must not be null.relation
- must not be null.- Returns:
- will never be null.
-
embed
Embeds the givenStream
in theRepresentationModel
. The givenStream
will be resolved immediately, i.e. the method acts as mere syntactic sugar to avoid clients having to collect theStream
into aCollection
beforehand. If theStream
is empty nothing will be added to theRepresentationModel
.- Parameters:
stream
- must not be null.- Returns:
- will never be null.
-
embed
Embeds the givenStream
in theRepresentationModel
and theLinkRelation
derived from the given type. The givenStream
will be resolved immediately, i.e. the method acts as mere syntactic sugar to avoid clients having to collect theStream
into aCollection
beforehand. If theStream
is empty nothing will be added to theRepresentationModel
an empty embed for the derivedLinkRelation
will be added.- Parameters:
stream
- must not be null.type
- the type to derive theLinkRelation
to be used, must not be null.- Returns:
- will never be null.
- See Also:
-
embed
Embeds the givenStream
in theRepresentationModel
for the givenLinkRelation
. The givenStream
will be resolved immediately, i.e. the method acts as mere syntactic sugar to avoid clients having to collect theStream
into aCollection
beforehand. If theStream
is empty nothing will be added to theRepresentationModel
an empty embed for the derivedLinkRelation
will be added.- Parameters:
stream
- must not be null.relation
- must not be null.- Returns:
- will never be null.
-
preview
Initiates the setup of a preview given the current payload. Clients have to conclude the setup calling any of theHalModelBuilder.PreviewBuilder.forLink(Link)
methods. As an example, the call chain of:….preview(…).forLink("…", "relation")
will result in the link added to the representation and an embedded being registered for the link's relation:{ "_links" : { "relation" : { … } }, "_embedded" : { "relation" : … } }
- Parameters:
entity
-- Returns:
- will never be null.
-
preview
Starts a preview setup for the givenCollection
as preview.- Parameters:
collection
-- Returns:
- will never be null.
- See Also:
-
preview
Starts a preview setup for the givenCollection
as preview falling back to the given type if theCollection
is empty.- Parameters:
collection
- must not be null.type
- must not be null.- Returns:
- will never be null.
- See Also:
-
link
Add aLink
to the whole thing. NOTE: This adds it to the top level. If you need a link inside an entity, then use theHalModelBuilder
to define it as well.- Parameters:
link
- must not be null.- Returns:
- will never be null.
-
link
- 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.
-
build
Transform the entities andLink
s into aRepresentationModel
. If there are embedded entries, add a preferred media type ofMediaTypes.HAL_JSON
andMediaTypes.HAL_FORMS_JSON
.- Returns:
- will never be null.
-