Spring Data Rest Neo4j输出中未在引用实体上公开的非关系属性

时间:2014-04-04 15:55:38

标签: spring-data-neo4j spring-data-rest neo4j spring-hateoas

我正在研究一些使用spring-data-neo4j(SDN)和spring-data-rest-mvc来存储和访问具有循环特征的图形数据的代码。我使用spring-data-rest-mvc访问HATEOAS功能,允许客户端通过链接来浏览图表。

作为一个简单的例子,我从http://spring.io/guides/gs/accessing-neo4j-data-rest/获取了gs-access-neo4j-data-rest代码,并用一种​​社交方面增强了Person类,即" friends&# 34;为:

@RelatedTo(type = "FRIENDS_WITH", direction = Direction.BOTH)
Set<Person> friends = new HashSet<Person>();

即。 Person实例之间的双向关系。

我加载了一组示例数据,并在它们之间建立了随机的朋友关系。到目前为止一切都很好。

如果我点击

http://localhost:8080/people/408 

我得到了预期的结果:

{
  "firstName" : "Remona",
  "lastName" : "Heier",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/people/408"
    },
    "friends" : {
      "href" : "http://localhost:8080/people/408/friends"
    }
  }
}

如果我点击

http://localhost:8080/people/408/friends

然而,我得到了

{
  "_embedded" : {
    "people" : [ {
      "firstName" : null,
      "lastName" : null,
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/people/189"
        },
        "friends" : {
          "href" : "http://localhost:8080/people/189/friends"
        }
      }
    }, {
      "firstName" : null,
      "lastName" : null,
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/people/34"
        },
        "friends" : {
          "href" : "http://localhost:8080/people/34/friends"
        }
      }
    }
  }
}

包含firstName和lastName的空值。

直接击中其中一位朋友,例如

http://localhost:8080/people/189

我明白了:

{
  "firstName" : "Zita",
  "lastName" : "Speltz",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/people/189"
    },
    "friends" : {
      "href" : "http://localhost:8080/people/189/friends"
    }
  }
}

我理解为什么我得到空值 - 朋友集上没有@Fetch,所以朋友们实际上并没有获取 - 只有节点ID是已知的并且用于建立自我和朋友的hrefs。但是使用空值生成的输出是错误的 - 它是对数据的错误表示。

我不能在朋友Set上包含@Fetch,因为这会将应用程序发送到旋转并溢出堆栈,这样我就会陷入困境。我希望看到列出朋友时所显示的朋友的非关系属性,即我想看到:

{
  "_embedded" : {
    "people" : [ {
      "firstName" : "Zita",
      "lastName" : "Speltz",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/people/189"
        },
        "friends" : {
          "href" : "http://localhost:8080/people/189/friends"
        }
      }
    }, {
      "firstName" : "Ciara",
      "lastName" : "Cobbley",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/people/34"
        },
        "friends" : {
          "href" : "http://localhost:8080/people/34/friends"
        }
      }
    }
  }
}

有谁知道我怎么做到这一点?

非常感谢提前。

1 个答案:

答案 0 :(得分:1)

这似乎是spring-data或spring-data-rest中的错误。我使用spring-data-rest 2.3.0和spring-data-jpa在lazily-fetched实体上遇到了同样的事情。我能够获得您想要的输出的唯一方法,即实际显示的实体信息,是急切地获取相关实体。你说你不能这样做,所以我看到的唯一选择就是你编写自己的控制器来返回正确的数据。您只需覆盖引用实体的requestMappings,因为spring-data-rest控制器仍将处理您的控制器无法处理的任何requestMappings。 spring-restbucks项目在这里有一个自定义控制器的例子:

https://github.com/olivergierke/spring-restbucks/blob/master/src/main/java/org/springsource/restbucks/payment/web/PaymentController.java

您需要添加一种方法来处理&#34; / people / {id} / friends&#34; requestMapping,并使用数据和链接自行填充资源。