Customizing the JSON Output

有时在您的应用程序中,您需要从特定实体提供到其他资源的链接。例如,`Customer`响应可以用链接到当前购物车,或到管理与该实体相关的资源的链接来丰富。Spring Data REST 提供与 Spring HATEOAS的集成,并提供一个扩展挂钩,供您更改传递到客户端的资源的表示形式。

Sometimes in your application, you need to provide links to other resources from a particular entity. For example, a Customer response might be enriched with links to a current shopping cart or links to manage resources related to that entity. Spring Data REST provides integration with Spring HATEOAS and provides an extension hook that lets you alter the representation of resources that go out to the client.

The RepresentationModelProcessor Interface

Spring HATEOAS 定义了一个用于处理实体的 RepresentationModelProcessor<> 接口。Spring Data REST 导出程序将自动选取类型为 RepresentationModelProcessor<EntityModel<T>> 的所有 bean,并在对类型为 T 的实体进行序列化时触发这些 bean。

Spring HATEOAS defines a RepresentationModelProcessor<> interface for processing entities. All beans of type RepresentationModelProcessor<EntityModel<T>> are automatically picked up by the Spring Data REST exporter and triggered when serializing an entity of type T.

例如,为了为 Person 实体定义一个处理器,可以向 ApplicationContext 中添加一个类似于下面的 @Bean(取自 Spring Data REST 测试):

For example, to define a processor for a Person entity, add a @Bean similar to the following (which is taken from the Spring Data REST tests) to your ApplicationContext:

@Bean
public RepresentationModelProcessor<EntityModel<Person>> personProcessor() {

   return new RepresentationModelProcessor<EntityModel<Person>>() {

     @Override
     public EntityModel<Person> process(EntityModel<Person> model) {

      model.add(new Link("http://localhost:8080/people", "added-link"));
      return model;
     }
   };
}

前面示例将硬编码链接到 http://localhost:8080/people 。如果在应用内有一个希望链接的 Spring MVC 终端, 请考虑使用 Spring HATEOAS 的 linkTo(…​) 方法, 以免管理 URL 。

The preceding example hard codes a link to http://localhost:8080/people. If you have a Spring MVC endpoint inside your app to which you wish to link, consider using Spring HATEOAS’s linkTo(…​) method to avoid managing the URL.

你可以通过调用 model.add(Link) 向实体的默认表示添加链接,如前面的例子所示。你添加到 EntityModel 的任何链接都会被添加到最终输出。

You can add links to the default representation of an entity by calling model.add(Link), as the preceding example shows. Any links you add to the EntityModel are added to the final output.

Customizing the Representation

在创建输出表示之前,Spring Data REST 导出器会运行任何已发现的 RepresentationModelProcessor 实例。它通过在内部 ConversionService 中注册一个 Converter<Entity, EntityModel> 实例来实现这一点。此组件负责创建到引用实体的链接(例如对象 JSON 表示中的 _links 属性下的那些对象)。它获取一个 @Entity 并迭代它的属性,为那些由 Repository 管理的属性创建链接并复制任何嵌入式或简单属性。

The Spring Data REST exporter runs any discovered RepresentationModelProcessor instances before it creates the output representation. It does so by registering a Converter<Entity, EntityModel> instance with an internal ConversionService. This is the component responsible for creating the links to referenced entities (such as those objects under the _links property in the object’s JSON representation). It takes an @Entity and iterates over its properties, creating links for those properties that are managed by a Repository and copying across any embedded or simple properties.

但是,如果你的项目需要以不同的格式输出,你可以完全用你自己的默认出站 JSON 表示替换它。如果你在 ApplicationContext 中注册自己的 ConversionService 和自己的 Converter<Entity, EntityModel>,你可以返回你选择的 EntityModel 实现。

If your project needs to have output in a different format, however, you can completely replace the default outgoing JSON representation with your own. If you register your own ConversionService in the ApplicationContext and register your own Converter<Entity, EntityModel>, you can return a EntityModel implementation of your choosing.