Java 将链接添加到投影';s嵌套对象

Java 将链接添加到投影';s嵌套对象,java,spring,spring-data,spring-data-rest,Java,Spring,Spring Data,Spring Data Rest,我使用Spring Data REST的功能,以便在JSON中有一些嵌套类型化对象: { "id": 1, "name": "TEST", "user": { "id": 1, "name": "user1" }, _links: { self: { href: "http://localhost:8082/accounts/1{?projection}", templated: tr

我使用Spring Data REST的功能,以便在JSON中有一些嵌套类型化对象:

{
   "id": 1,
   "name": "TEST",
   "user": {
      "id": 1,
      "name": "user1"
   },
   _links: {
       self: {
           href: "http://localhost:8082/accounts/1{?projection}",
           templated: true
       },
       user: {
           href: "http://localhost:8082/accounts/1/users"
       },
    }
}
如何在嵌套对象内生成链接?我需要以下JSON表示:

{
       "id": 1,
       "name": "TEST",
       "user": {
          "id": 1,
          "name": "user1",
          _links: {
              self: {
                  href: "http://localhost:8082/users/1",
                  templated: true
              },
           }
       },
       _links: {
           self: {
               href: "http://localhost:8082/accounts/1{?projection}",
               templated: true
           },
           user: {
               href: "http://localhost:8082/accounts/1/users"
           },
        }
    }

注意:我看到了,但不知道如何在我的案例中使用它(如果可能的话)

我偶然发现了这个问题,正在寻找解决方案。在做了一些修改之后,我发现了如何做到这一点

我假设
Account
是您的根对象,您希望它有一个嵌套的
用户集合
,每个用户依次拥有
\u链接

1:为Users对象添加一个
摘录(这是一种隐藏列表集合中不重要细节的简便技术)

2:
摘录与您的
用户存储库关联起来

@RepositoryRestResource(excerptProjection = UserExcerpt.class)
public abstract interface UserRepository extends JpaRepository<User, Long> ...
这里重要的一点是,您的投影需要引用
userextract
,而不是
User
。这样,从
GET/accounts/projection=accountusersproject
返回的结果将如下所示:

{
  "_embedded" : {
    "accounts" : [ {
      "name" : "ProjA",
      "users" : [ {
        "name" : "Customer Admin",
        "email" : "customer@meshcloud.io",
        "_links" : {
          "self" : {
            "href" : "http://localhost:8080/users/2{?projection}",
            "templated" : true
          }, ...
        }
      } ], 
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/accounts/1"
        },
        ...
      }
    }  ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/accounts"
    },
    ...
  },
  "page" : {
    "size" : 50,
    "totalElements" : 2,
    "totalPages" : 1,
    "number" : 0
  }
}

您是否尝试过SpringDataREST的最新快照?最近我们对投影的默认链接渲染做了一些调整?@OliverGierke谢谢,我会检查一下的@OliverGierke你能告诉我应该去哪里吗?
@Projection(types = {Account.class})
public interface AccountUsersProjection {

    String getName();
    ...
    Set<UserExcerpt> getUsers();
}
{
  "_embedded" : {
    "accounts" : [ {
      "name" : "ProjA",
      "users" : [ {
        "name" : "Customer Admin",
        "email" : "customer@meshcloud.io",
        "_links" : {
          "self" : {
            "href" : "http://localhost:8080/users/2{?projection}",
            "templated" : true
          }, ...
        }
      } ], 
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/accounts/1"
        },
        ...
      }
    }  ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/accounts"
    },
    ...
  },
  "page" : {
    "size" : 50,
    "totalElements" : 2,
    "totalPages" : 1,
    "number" : 0
  }
}