5. Configuration

This section describes how to configure Spring HATEOAS.spring-doc.cn

5.1. Using @EnableHypermediaSupport

To let the RepresentationModel subtypes be rendered according to the specification of various hypermedia representation types, you can activate support for a particular hypermedia representation format through @EnableHypermediaSupport. The annotation takes a HypermediaType enumeration as its argument. Currently, we support HAL as well as a default rendering. Using the annotation triggers the following:spring-doc.cn

  • It registers necessary Jackson modules to render EntityModel and CollectionModel in the hypermedia specific format.spring-doc.cn

  • If JSONPath is on the classpath, it automatically registers a LinkDiscoverer instance to look up links by their rel in plain JSON representations (see Using LinkDiscoverer Instances).spring-doc.cn

  • By default, it enables entity links and automatically picks up EntityLinks implementations and bundles them into a DelegatingEntityLinks instance that you can autowire.spring-doc.cn

  • It automatically picks up all RelProvider implementations in the ApplicationContext and bundles them into a DelegatingRelProvider that you can autowire. It registers providers to consider @Relation on domain types as well as Spring MVC controllers. If the EVO inflector is on the classpath, collection rel values are derived by using the pluralizing algorithm implemented in the library (see [spis.rel-provider]).spring-doc.cn

5.1.1. Explicitly enabling support for dedicated web stacks

By default, @EnableHypermediaSupport will reflectively detect the web application stack you’re using and hook into the Spring components registered for those to enable support for hypermedia representations. However, there are situations in which you’d only explicitly want to activate support for a particular stack. E.g. if your Spring WebMVC based application uses WebFlux' WebClient to make outgoing requests and that one is not supposed to work with hypermedia elements, you can restrict the functionality to be enabled by explicitly declaring WebMVC in the configuration:spring-doc.cn

Example 42. Explicitly activating hypermedia support for a particular web stack
@EnableHypermediaSupport(…, stacks = WebStack.WEBMVC)
class MyHypermediaConfiguration { … }