在 Spring Data Rest 响应中选择性地扩展关联

2024-08-24Java开发问题
15

本文介绍了在 Spring Data Rest 响应中选择性地扩展关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个标准的 Spring 数据 JPA 和 Spring 数据 Rest 设置,它们正确地将关联作为指向正确资源的链接返回.

I have a standard Spring data JPA and Spring data Rest setup which, correctly, returns associations as links to the correct resources.

{
    "id": 1,
    "version": 2,
    "date": "2011-11-22",
    "description": "XPTO",
    "_links": {
        "self": {
            "href": "http://localhost:8000/api/domain/1"
        },
        "otherDomain": {
            "href": "http://localhost:8000/api/domain/1/otherDomain"
        }
    }
}   

但是,在 一些 请求中,我希望扩展与otherDomain"的关联(因此客户端不必执行 N+1 次请求即可获取完整数据).

However in some requests i would like to have the association to the "otherDomain" expanded (so the client does not have to do N+1 requests to get the full data).

是否可以配置 Spring Data Rest 以这种方式处理响应?

Is it possible to configure Spring Data Rest to handle the response in this way?

推荐答案

默认响应必须保持不变,以确保 PUT 请求的负载与 GET 的负载对称 的回报.但是,Spring Data REST 引入了一个称为投影的功能(有关详细信息,请参阅 JIRA 票证)其工作原理如下:

The default responses will have to stay the same to make sure the payloads for PUT requests are symmetric to the ones GETs return. However, Spring Data REST introduces a feature called projections (see the JIRA ticket for details) that works as follows:

您创建一个专用接口并添加您想要包含在响应中的所有属性:

You create a dedicated interface and add all properties that you want to include in the response:

public interface MyProjection {

  String getMyProperty();

  MyRelatedObject getOtherDomain();
}

你可以

  • 使用 @Projection 注释接口并将其放置在与域类型或其子包相同的包中
  • 或者您使用 RepositoryRestConfiguration 手动注册投影并手动调用 projectionConfiguration().addProjection(…)(通过扩展 RepositoryRestMvcConfiguration 并覆盖configureRepositoryRestConfiguration(…)).
  • annotate the interface using @Projection and place it in the very same package as the domain type or a subpackage of it
  • or you manually register the projection using the RepositoryRestConfiguration and call projectionConfiguration().addProjection(…) manually (by extending RepositoryRestMvcConfiguration and overriding configureRepositoryRestConfiguration(…)).

这将导致为域类型公开的资源接受带有投影名称的 projection 参数(名称也可配置 ProjectionConfiguration).如果给定,我们将跳过默认渲染(包括渲染相关实体的链接而不是嵌入它们)并让 Jackson 渲染支持给定接口的代理.

This will cause the resources exposed for the domain type to accept a projection parameter (name also configurable ProjectionConfiguration) with the name of the projection. If given, we will skip the default rendering (which includes rendering links to related entities instead of embedding them) and let Jackson render a proxy backing the given interface.

在 Spring RESTBucks 项目中也可以找到一个示例.请参阅 OrderProjection 用于接口定义.

An example of that can also be found in the Spring RESTBucks project. See the OrderProjection for the interface definition.

这篇关于在 Spring Data Rest 响应中选择性地扩展关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13