我有一个名为VeiculoAgencia的类,其中Veiculo作为参考Agencia。当它不是列表时,我可以传递URI引用以及以下example。但是我怎么办清单呢?我很高兴有人能帮助我

没有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();
}


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);
}


卷曲

在/ 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:否
  D b;嵌套的异常是com.mongodb.MongoInternalException:处没有数据库
  org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:77)
    在
  org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2011)
    在
  org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:1841)
    在
  org.springframework.data.mongodb.core.MongoTemplate.doFindOne(MongoTemplate.java:1654)
    在
  org.springframework.data.mongodb.core.MongoTemplate.findById(MongoTemplate.java:613)
    在
  org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findOne(SimpleMongoRepository.java:119)
    在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    在
  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    在java.lang.reflect.Method.invoke(Method.java:497)在
  org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:452)
    在
  org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:437)
    在
  org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:409)
    在
  org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    在
  org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
    在
  org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    在
  org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    在com.sun.proxy。$ Proxy71.findOne(未知来源)处
  org.springframework.data.repository.support.CrudRepositoryInvoker.invokeFindOne(CrudRepositoryInvoker.java:91)
    在
  org.springframework.data.rest.core.support.UnwrappingRepositoryInvokerFactory $ UnwrappingRepositoryInvoker.invokeFindOne(UnwrappingRepositoryInvokerFactory.java:133)
    在
  org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController.doWithReferencedProperty(RepositoryPropertyReferenceController.java:437)
    在
  org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController.createPropertyReference(RepositoryPropertyReferenceController.java:352)
    在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    在
  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    在java.lang.reflect.Method.invoke(Method.java:497)在
  org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    在
  org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    在
  org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)
    在
  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:799)
    在
  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:728)
    在
  org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    在
  org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    在
  org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    在
  org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
    在
  org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:882)
    在javax.servlet.http.HttpServlet.service(HttpServlet.java:651)处
  org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
    在javax.servlet.http.HttpServlet.service(HttpServlet.java:729)在
  org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    在
  org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    在
  org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    在
  org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    在
  org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    在
  org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:316)
    在
  org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
    在
  org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
    在
  org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    在
  org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
    在
  org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    在
  org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:122)
    在
  org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    在
  org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    在
  org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    在
  org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:168)
    在
  org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    在
  org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:48)
    在
  org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    在
  org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120)
    在
  org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    在
  org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
    在
  org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    在
  org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    在
  org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)
    在
  org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    在
  org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53)
    在
  org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    在
  org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    在
  org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213)
    在
  org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176)
    在
  org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    在
  org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    在
  org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
    在
  org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    在
  org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    在
  org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    在
  org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    在
  org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    在
  org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    在
  org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    在
  org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    在
  org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    在
  org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    在
  org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    在
  org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:668)
    在
  org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1521)
    在
  org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1478)
    在
  java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    在
  java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)
    在
  org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)
    在java.lang.Thread.run(Thread.java:745)造成原因:
  com.mongodb.MongoInternalException:处没有数据库
  com.mongodb.DBRefBase.fetch(DBRefBase.java:86)在
  sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于
  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    在
  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    在java.lang.reflect.Method.invoke(Method.java:497)在
  org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:211)
    在
  org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:196)
    在
  org.springframework.data.mongodb.core.convert.ReflectiveDBRefResolver.fetch(ReflectiveDBRefResolver.java:64)
    在
  org.springframework.data.mongodb.core.convert.DefaultDbRefResolver.fetch(DefaultDbRefResolver.java:109)
    在
  org.springframework.data.mongodb.core.convert.MappingMongoConverter.readRef(MappingMongoConverter.java:1214)
    在
  org.springframework.data.mongodb.core.convert.MappingMongoConverter.readCollectionOrArray(MappingMongoConverter.java:904)
    在
  org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1183)
    在
  org.springframework.data.mongodb.core.convert.MappingMongoConverter.access $ 200(MappingMongoConverter.java:78)
    在
  org.springframework.data.mongodb.core.convert.MappingMongoConverter $ MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1133)
    在
  org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:869)
    在
  org.springframework.data.mongodb.core.convert.DefaultDbRefResolverCallback.resolve(DefaultDbRefResolverCallback.java:59)
    在
  org.springframework.data.mongodb.core.convert.DefaultDbRefResolver.resolveDbRef(DefaultDbRefResolver.java:90)
    在
  org.springframework.data.mongodb.core.convert.MappingMongoConverter $ 2.doWithAssociation(MappingMongoConverter.java:304)
    在
  org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:339)
    在
  org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:287)
    在
  org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:231)
    在
  org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:191)
    在
  org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:187)
    在
  org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:78)
    在
  org.springframework.data.mongodb.core.MongoTemplate $ ReadDbObjectCallback.doWith(MongoTemplate.java:2200)
    在
  org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:1837)
    ...省略了88个通用框架

最佳答案

VeiculoContato提供资源的Agencia实例公开指向association resource的链接。您可以向它们发布媒体类型为text/uri-list的有效负载,以为Veiculo分配引用。

关于java - @DBRef与Spring Data Rest,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31521565/

10-15 07:15