Spring HATEOAS
如果开发利用超媒体的 RESTful API,则 Spring Boot 会为 Spring HATEOAS 提供自动配置,它适用于大多数应用程序。自动配置消除了使用 @EnableHypermediaSupport
的需要,并注册了许多 bean 以简化基于超媒体的应用程序的构建,包括一个 LinkDiscoverers
(用于客户端支持)和一个配置为以所需表示正确编组响应的 ObjectMapper
。通过设置各种 spring.jackson.*
属性或(如果存在则)通过 Jackson2ObjectMapperBuilder
bean 自定义 ObjectMapper
。
If you develop a RESTful API that makes use of hypermedia, Spring Boot provides auto-configuration for Spring HATEOAS that works well with most applications.
The auto-configuration replaces the need to use @EnableHypermediaSupport
and registers a number of beans to ease building hypermedia-based applications, including a LinkDiscoverers
(for client side support) and an ObjectMapper
configured to correctly marshal responses into the desired representation.
The ObjectMapper
is customized by setting the various spring.jackson.*
properties or, if one exists, by a Jackson2ObjectMapperBuilder
bean.
可以使用 @EnableHypermediaSupport
控制 Spring HATEOAS 的配置。请注意,这样做会禁用先前描述的 ObjectMapper
自定义。
You can take control of Spring HATEOAS’s configuration by using @EnableHypermediaSupport
.
Note that doing so disables the ObjectMapper
customization described earlier.
spring-boot-starter-hateoas
适用于 Spring MVC,不应与 Spring WebFlux 结合使用。为了在 Spring WebFlux 中使用 Spring HATEOAS,可以添加对 org.springframework.hateoas:spring-hateoas
和 spring-boot-starter-webflux
的直接依赖。
spring-boot-starter-hateoas
is specific to Spring MVC and should not be combined with Spring WebFlux.
In order to use Spring HATEOAS with Spring WebFlux, you can add a direct dependency on org.springframework.hateoas:spring-hateoas
along with spring-boot-starter-webflux
.
默认情况下,接受 application/json
的请求会收到 application/hal+json
响应。要禁用此行为,请将 configprop:spring.hateoas.use-hal-as-default-json-media-type[] 设置为 false
,并定义一个 HypermediaMappingInformation
或 HalConfiguration
以配置 Spring HATEOAS 以满足您的应用程序及其客户端的需求。
By default, requests that accept application/json
will receive an application/hal+json
response.
To disable this behavior set configprop:spring.hateoas.use-hal-as-default-json-media-type[] to false
and define a HypermediaMappingInformation
or HalConfiguration
to configure Spring HATEOAS to meet the needs of your application and its clients.