【问题标题】:Spring Data Rest Mongo - how to create a DBRef using an id instead of a URI?Spring Data Rest Mongo - 如何使用 id 而不是 URI 创建 DBRef?
【发布时间】:2016-05-03 10:24:09
【问题描述】:

我有以下实体,它引用了另一个实体。

class Foo {
    String id;
    String name supplierName;
    **@DBRef** TemplateSchema templateSchema;
...
}

我希望能够使用以下 JSON(或类似的)来创建新实体。

{
    "supplierName": "Stormkind",
    "templateSchema": "572878138b749120341e6cbf"
}

...但看起来 Spring 强制您使用这样的 URI:

{
    "supplierName": "Stormkind",
    "templateSchema": "/template-schema/572878138b749120341e6cbf"
}

有没有办法通过发布 ID 而不是 URI 来创建 DBRef?

谢谢!

【问题讨论】:

  • 您可以为 DBRef 实现自定义反序列化器,但正如 Oliver Gierke 指出的那样,REST 资源应该由它们的 URI 来标识。

标签: spring spring-data-mongodb spring-data-rest


【解决方案1】:

在 REST 中,唯一存在的 ID 形式是 URI(因此称为唯一资源标识符)。像 572878138b749120341e6cbf 这样的东西不能识别资源,/template-schema/572878138b749120341e6cbf 可以。

在 HTTP 级别上,不存在实体,只有通过 URI 标识的资源。这就是 Spring Data REST 期望您使用 URI 作为标识符的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-29
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 2017-05-29
    • 1970-01-01
    • 2017-10-30
    相关资源
    最近更新 更多