当前位置: 首页 > 知识库问答 >
问题:

@dbref带有Spring Data Rest

景岳
2023-03-14

我有一个名为veiculoagencia的类,其中veiculo拥有作为引用的agencia。当它不是列表时,我可以传递URI引用以及以下示例。但我怎么做当一个清单?如果有人能帮助我,我很感激

没有getter和setter的实体

细静脉

@Document
public class Veiculo{

    @Id
    private String id;

    @Indexed(unique = true)
    private String nome;
    private String tipo;
    @DBRef
    List<Contato> contatos;

    @DBRef
    List<Agencia> agencias;

}

阿根西亚

@Document
public class Agencia {

    @Id
    String id;
    @NotNull
    String nome;

    @CreatedBy
    String createdBy;

    @LastModifiedBy
    String lastModifiedBy;

    @CreatedDate
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    Date createdAt;

    @LastModifiedDate
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    Date lastModified;
}

静脉储存库

@RepositoryRestResource(collectionResourceRel = "veiculos", path = "veiculos")
public interface VeiculoRepository extends MongoRepository<Veiculo, String> {
    Veiculo save(Veiculo veiculo);
    List<Veiculo> findAll();
}

Agencia存储库

@RepositoryRestResource(collectionResourceRel = "agencias", path = "agencias")
public interface AgenciaRepository extends MongoRepository<Agencia, String> {

    Agencia save(Agencia t);
    List<Agencia> findAll();
    Agencia findByNome(@Param("nome") String nome);
}
daniela@daniela-tars:~$ curl -i -X POST -H "Content-Type: application/json" -d '{"nome": "Agencia"}' localhost:8181/api/agencias
HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Last-Modified: Tue, 21 Jul 2015 01:58:40 GMT
Last-Modified: Tue, 21 Jul 2015 01:58:40 GMT
Location: http://localhost:8181/api/agencias/55ada75044ae41ca763aa3b4
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 21 Jul 2015 01:58:40 GMT

{
  "nome" : "Agencia",
  "createdBy" : "anonymousUser",
  "lastModifiedBy" : "anonymousUser",
  "createdAt" : "2015-07-21T01:58:40.021+0000",
  "lastModified" : "2015-07-21T01:58:40.021+0000",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8181/api/agencias/55ada75044ae41ca763aa3b4"
    }
  }
}
daniela@daniela-tars:~$ curl -i -X POST -H "Content-Type: application/json" -d '{"nome": "Veiculo", "tipo": "Tipo"}' localhost:8181/api/veiculosHTTP/1.1 201 Created
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Location: http://localhost:8181/api/veiculos/55ada77344ae41ca763aa3b7
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 21 Jul 2015 01:59:15 GMT

{
  "nome" : "Veiculo",
  "tipo" : "Tipo",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8181/api/veiculos/55ada77344ae41ca763aa3b7"
    },
    "contatos" : {
      "href" : "http://localhost:8181/api/veiculos/55ada77344ae41ca763aa3b7/contatos"
    },
    "agencias" : {
      "href" : "http://localhost:8181/api/veiculos/55ada77344ae41ca763aa3b7/agencias"
    }
  }
}
    daniela@daniela-tars:~$ curl -i -X PUT -H "Content-Type: text/uri-list" -d "http://localhost:8181/api/agencias/55ada75044ae41ca763aa3b4" http://localhost:8181/api/veiculos/55ada77344ae41ca763aa3b7/agencias
    HTTP/1.1 204 No Content
    Server: Apache-Coyote/1.1
    X-Content-Type-Options: nosniff
    X-XSS-Protection: 1; mode=block
    Cache-Control: no-cache, no-store, max-age=0, must-revalidate
    Pragma: no-cache
    Expires: 0
    X-Frame-Options: DENY
    Date: Tue, 21 Jul 2015 02:00:13 GMT
    daniela@daniela-tars:~$ curl localhost:8181/api/veiculos
    {"timestamp":1437444045504,"status":500,"error":"Internal Server Error","exception":"org.springframework.dao.InvalidDataAccessResourceUsageException","message":"no db; nested exception is com.mongodb.MongoInternalException: no db","path":"/api/veiculos"}
daniela@daniela-tars:~$ curl localhost:8181/api/veiculos/55ada77344ae41ca763aa3b7
    {"timestamp":1437444071205,"status":500,"error":"Internal Server Error","exception":"org.springframework.dao.InvalidDataAccessResourceUsageException","message":"no db; nested exception is com.mongodb.MongoInternalException: no db","path":"/api/veiculos/55ada77344ae41ca763aa3b7"}

共有1个答案

逑阳泽
2023-03-14

服务于veiculo实例的资源为contatoagencia公开到关联资源的链接。您可以将媒体类型为text/uri-list的有效负载发布给它们,以分配对veiculo的引用。

 类似资料:
  • 我写代码时遇到了麻烦,这将允许以一种简单明了的方式获取用户和索赔细节。这是我的MongoDB结构, 我的实体类是: 索赔类别: 我有一种方法可以按名称获取用户,如下所示, 如果我尝试使用这个方法,会得到一个错误, 找不到能够从类型<code>org.bson.types转换的转换器。ObjectId以键入 所以我更改了我的用户实体类,如下所示, 而不是

  • 我的应用程序使用Spring Boot/JPA/MongoDB。 我使用以下方法将域类映射到MongoDB 但找不到如何使我的DBRef字段唯一。

  • Mongoose似乎不支持Mongo DBREF。显然,他们发布了“dbref”支持,但它实际上只是简单的引用(不具备引用不同集合中的文档的能力)。我终于创建了一个模式,允许我保存ObjectID引用的数组并填充它们,这对于我的模式的某些部分是很好的,但是如果我可以使用适当的DBREF创建一个数组,允许我引用来自多个集合的文档,那将是非常方便的。 幸运地(?)有一个模块可以在Mongoose中修补

  • 问题内容: 我在MongoDB中的规范化数据模型结构中遇到以下错误: 这是由于以下原因造成的: 具体的部分。我的文档中有一个DBRef对象,因此我可以引用另一个集合中的文档。嵌入式文档结构不是选项。那么我该如何解决呢? 问题答案: 您必须为其导入DBRef编解码器才能进行打印,如果您希望以文档json样式进行打印,则需要编写自己的DBRef编解码器,并将其添加到您给toJson()的编解码器中。

  • 我试图使用在模型中创建一个列表,但我无法使其工作。这是我的用户模型: 服务器型号: 结构非常简单,每个用户可以有多个服务器。但是,当我向用户添加服务器时,服务器就创建了,但是servers数组包含一个条目()。因此服务器不会添加到用户中。这就是我如何创建服务器并将其添加到用户: 所以我只需创建并保存一个服务器,将服务器添加到用户,然后保存用户。但不管用。我一直有一个数组,其中有一个项。 我所有的库

  • 代码: 上下文:尝试在JavaScript中使用 编辑: 完整代码: 编辑: 无法获取