<bdo id='g0VN1'></bdo><ul id='g0VN1'></ul>

<i id='g0VN1'><tr id='g0VN1'><dt id='g0VN1'><q id='g0VN1'><span id='g0VN1'><b id='g0VN1'><form id='g0VN1'><ins id='g0VN1'></ins><ul id='g0VN1'></ul><sub id='g0VN1'></sub></form><legend id='g0VN1'></legend><bdo id='g0VN1'><pre id='g0VN1'><center id='g0VN1'></center></pre></bdo></b><th id='g0VN1'></th></span></q></dt></tr></i><div id='g0VN1'><tfoot id='g0VN1'></tfoot><dl id='g0VN1'><fieldset id='g0VN1'></fieldset></dl></div>

  1. <legend id='g0VN1'><style id='g0VN1'><dir id='g0VN1'><q id='g0VN1'></q></dir></style></legend>

  2. <tfoot id='g0VN1'></tfoot>
  3. <small id='g0VN1'></small><noframes id='g0VN1'>

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

      时间:2024-08-24
    1. <legend id='9p102'><style id='9p102'><dir id='9p102'><q id='9p102'></q></dir></style></legend>
        <tbody id='9p102'></tbody>

          <bdo id='9p102'></bdo><ul id='9p102'></ul>
        • <tfoot id='9p102'></tfoot>

                <i id='9p102'><tr id='9p102'><dt id='9p102'><q id='9p102'><span id='9p102'><b id='9p102'><form id='9p102'><ins id='9p102'></ins><ul id='9p102'></ul><sub id='9p102'></sub></form><legend id='9p102'></legend><bdo id='9p102'><pre id='9p102'><center id='9p102'></center></pre></bdo></b><th id='9p102'></th></span></q></dt></tr></i><div id='9p102'><tfoot id='9p102'></tfoot><dl id='9p102'><fieldset id='9p102'></fieldset></dl></div>

                <small id='9p102'></small><noframes id='9p102'>

                本文介绍了在 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 响应中选择性地扩展关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在 Spring Boot 中使用 JNDI 配置多个数据源 下一篇:Org.Hibernate.AnnotationException:没有为实体指定标识符我的表中没有 id

                相关文章

              • <i id='UoEN8'><tr id='UoEN8'><dt id='UoEN8'><q id='UoEN8'><span id='UoEN8'><b id='UoEN8'><form id='UoEN8'><ins id='UoEN8'></ins><ul id='UoEN8'></ul><sub id='UoEN8'></sub></form><legend id='UoEN8'></legend><bdo id='UoEN8'><pre id='UoEN8'><center id='UoEN8'></center></pre></bdo></b><th id='UoEN8'></th></span></q></dt></tr></i><div id='UoEN8'><tfoot id='UoEN8'></tfoot><dl id='UoEN8'><fieldset id='UoEN8'></fieldset></dl></div>

                  <bdo id='UoEN8'></bdo><ul id='UoEN8'></ul>
              • <tfoot id='UoEN8'></tfoot>

                <legend id='UoEN8'><style id='UoEN8'><dir id='UoEN8'><q id='UoEN8'></q></dir></style></legend>

                  1. <small id='UoEN8'></small><noframes id='UoEN8'>