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
Nested Classes -
Method Summary
Modifier and TypeMethodDescription<T extends RepresentationModel<T>>
RepresentationModel<T>build()Transform the entities andLinks 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 theRepresentationModeland theLinkRelationderived from the given type.embed(Collection<?> collection, LinkRelation relation) Embeds the given collection in theRepresentationModelfor the givenLinkRelation.Embeds the givenStreamin theRepresentationModel.Embeds the givenStreamin theRepresentationModeland theLinkRelationderived from the given type.embed(Stream<?> stream, LinkRelation relation) static HalModelBuilderCreates a newHalModelBuilderwithout a primary payload.Embed the entity, but with no relation.static HalModelBuilderhalModel()Creates a newHalModelBuilder.static HalModelBuilderhalModel(EmbeddedWrappers wrappers) Creates a newHalModelBuilderusing the givenEmbeddedWrappers.static HalModelBuilderhalModelOf(Object entity) Creates a newHalModelBuilderwith the given entity as primary payload.link(String href, LinkRelation relation) Add aLinkto the whole thing.Adds the givenLinks to theRepresentationModelto be built.Initiates the setup of a preview given the current payload.preview(Collection<?> collection) Starts a preview setup for the givenCollectionas preview.preview(Collection<?> collection, Class<?> type) Starts a preview setup for the givenCollectionas preview falling back to the given type if theCollectionis empty.
-
Method Details
-
halModel
Creates a newHalModelBuilder.- Returns:
- will never be null.
-
halModel
Creates a newHalModelBuilderusing the givenEmbeddedWrappers.- Parameters:
wrappers- must not be null.- Returns:
- will never be null.
-
halModelOf
Creates a newHalModelBuilderwith the given entity as primary payload.- Parameters:
entity- must not be null.- Returns:
-
emptyHalModel
Creates a newHalModelBuilderwithout 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 theRepresentationModeland theLinkRelationderived from the given type. If the collection is empty nothing will be added to theRepresentationModelan empty embed for the derivedLinkRelationwill be added.- Parameters:
collection- must not be null.type- the type to derive theLinkRelationto be used, must not be null.- Returns:
- will never be null.
- See Also:
-
embed
Embeds the given collection in theRepresentationModelfor the givenLinkRelation. If the collection is empty nothing will be added to theRepresentationModelan empty embed for the derivedLinkRelationwill be added.- Parameters:
collection- must not be null.relation- must not be null.- Returns:
- will never be null.
-
embed
Embeds the givenStreamin theRepresentationModel. The givenStreamwill be resolved immediately, i.e. the method acts as mere syntactic sugar to avoid clients having to collect theStreaminto aCollectionbeforehand. If theStreamis empty nothing will be added to theRepresentationModel.- Parameters:
stream- must not be null.- Returns:
- will never be null.
-
embed
Embeds the givenStreamin theRepresentationModeland theLinkRelationderived from the given type. The givenStreamwill be resolved immediately, i.e. the method acts as mere syntactic sugar to avoid clients having to collect theStreaminto aCollectionbeforehand. If theStreamis empty nothing will be added to theRepresentationModelan empty embed for the derivedLinkRelationwill be added.- Parameters:
stream- must not be null.type- the type to derive theLinkRelationto be used, must not be null.- Returns:
- will never be null.
- See Also:
-
embed
Embeds the givenStreamin theRepresentationModelfor the givenLinkRelation. The givenStreamwill be resolved immediately, i.e. the method acts as mere syntactic sugar to avoid clients having to collect theStreaminto aCollectionbeforehand. If theStreamis empty nothing will be added to theRepresentationModelan empty embed for the derivedLinkRelationwill 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 givenCollectionas preview.- Parameters:
collection-- Returns:
- will never be null.
- See Also:
-
preview
Starts a preview setup for the givenCollectionas preview falling back to the given type if theCollectionis empty.- Parameters:
collection- must not be null.type- must not be null.- Returns:
- will never be null.
- See Also:
-
link
Add aLinkto the whole thing. NOTE: This adds it to the top level. If you need a link inside an entity, then use theHalModelBuilderto 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 givenLinks to theRepresentationModelto be built.- Parameters:
links- must not be null.- Returns:
- will never be null.
-
build
Transform the entities andLinks into aRepresentationModel. If there are embedded entries, add a preferred media type ofMediaTypes.HAL_JSONandMediaTypes.HAL_FORMS_JSON.- Returns:
- will never be null.
-