我有一个名为Veiculo
and的类Agencia
,其中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;
}
Veiculo 存储库
@RepositoryRestResource(collectionResourceRel = "veiculos", path = "veiculos")
public interface VeiculoRepository extends MongoRepository<Veiculo, String> {
Veiculo save(Veiculo veiculo);
List<Veiculo> findAll();
}
通讯社资料库
@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);
}
卷曲
在 /api/agencias 中发布
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"
}
}
}
在 /api/veiculos 中发布
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"
}
}
}
关联 URI
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"}
日志
org.springframework.dao.InvalidDataAccessResourceUsageException:没有数据库;嵌套异常是 com.mongodb.MongoInternalException: no db at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:77) at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java :2011) 在 org.springframework.data.mongodb.core.MongoTemplate.doFindOne(MongoTemplate.java:1654) 在 org.springframework 的 org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:1841)。 data.mongodb.core.MongoTemplate.findById(MongoTemplate.java:613) 在 org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findOne(SimpleMongoRepository.java:119) 在 sun.reflect.NativeMethodAccessorImpl。