2015-07-20 173 views
1

我有一個類叫做VeiculoAgencia,其中Veiculo擁有作爲參考Agencia。當它不是一個列表,我可以通過的URI參考以及以下example。但是如何在列表中執行操作?我很感激,如果有人能幫助我@ DBRef與彈簧數據休息

實體沒有getter和setter

Veiculo

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

Agenc在/ API/veiculos

[email protected]:~$ 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

[email protected]:~$ 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 
    [email protected]:~$ 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"} 
[email protected]:~$ 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"} 
IA庫

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

捲曲

在POST/API/agencias

[email protected]:~$ 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" 
    } 
    } 
} 

POST

登錄

org.springframework.dao.InvalidDataAccessResourceUsageException:沒有 分貝;嵌套異常是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) at java.lang.reflect.Method.invoke(Method.java:497)at org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:452) at org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:437) at org.springfra mework.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) 在 有機.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 在 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 在com.sun.proxy。$ Proxy71.findOne (Unknown Source) org.springframework.data.repository.support.CrudRepositoryInvoker.invokeFindOne(CrudRepositoryInvoker.java:91) at 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) at 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) at 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) 在 有機.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) at javax.servlet.http.HttpServlet.service(HttpServlet.java:651)at org.springframework.web.servlet.FrameworkServlet.service(Framewo rkServlet.java:845) 處 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291 javax.servlet.http.HttpServlet.service(HttpServlet.java:729)) 在 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) at 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) 在 組織.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) at org.springframework.security.w eb.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) at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) at 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) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFi lter.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) at 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) 在 有機.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) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at 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) at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:668) at 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:沒有db在 com.mongodb.DBRefBase.fetch(DBRefBase.java:86)在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native方法)在 sun.reflect.NativeM ethodAccessorImpl.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) at org.springframework.data.mongodb.core.convert.DefaultDbRefResolver.fetch(DefaultDbRefResolver.java:109) at org.springframework.d ata.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) at 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) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:287) at 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共同框架省略

回答

1

服務Veiculo實例資源暴露兩個ContatoAgencia鏈接到association resource。您可以使用媒體類型text/uri-list向它們發送有效負載以分配對Veiculo的引用。

+0

這發生在我做出/ veiculos,/ veiculos/{ID} GET請求和/ veiculos/{ID}/agencias /(編號):'{ 「時間戳」:1437414418319, 「狀態」:500 , 「error」:「Internal Server Error」, 「exception」:「org.springframework.dao.InvalidDataAccessResourceUsageException」, 「message」:「no db;嵌套異常是com.mongodb.MongoInternalException:no db」, 「path」:「/ api/veiculos/55ad30e8ccf29fbf6011411e/agencias」 }' –

+0

問題是,應用程序運行Fongo時,mongoDB在內存中 –