一开始我想强调的是,如果解决方案没有解决我的问题,那么已经提出了类似的问题。 我是Mongodb的新手,因此我遇到了问题。
我想要做的是编写一个方法来检索名为' shopRepository'的集合中的所有商店。哪个"类别" field(即字符串数组)包含作为方法参数给出的类别。
shopRepository集合的文档结构结构如下:
{
"name": "corte",
"openingHours": "Mi-Fr 8-18",
"categories": ["food", "drinks"],
"mallId": "123",
"longitude": "1",
"latitude": "2",
"ownerId": "123x"
}
我写的方法看起来像这样:
public List<Shop> getByCategory(String category) {
BasicDBObject query = new BasicDBObject("categories", new BasicDBObject("$all", Arrays.asList(category)));
List<Shop> shops = new ArrayList<Shop>();
try (MongoCursor<Shop> cursor = collection.find(query).iterator()) {
while (cursor.hasNext()) {
shops.add(cursor.next());
}
}
return shops;
}
当我调用方法时,我收到HTTP状态500 - java.lang.NullPointerException
更新:
这是堆栈跟踪:
SEVERE: Servlet.service() for servlet [Jersey REST Service] in context with path [/WebServiceProject] threw exception [java.lang.NullPointerException] with root cause java.lang.NullPointerException
at restLayer.ShopResource.getShopByCategory(ShopResource.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:76)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:148)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:191)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:200)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:103)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:493)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:415)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:104)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:277)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:272)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:268)
at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
at org.glassfish.jersey.internal.Errors.process(Errors.java:268)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:289)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:256)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:703)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:416)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
ShopResource.getShopByCategory是我调用&#39; getByCategory&#39;的方法。上述方法。如果它有帮助,我也包括它:
@GET
@Path("category/{category}")
@Produces({ MediaType.APPLICATION_JSON})
public Response getShopByCategory(@PathParam("category") String category) {
List<Shop> shops = shopRepository.getByCategory(category);
if (shops == null) {
return Response.status(Status.NOT_FOUND).build();
}
List<Container> response = new ArrayList<Container>();
for(Shop q : shops) {
response.add((Container) stripAttributes(q));
}
return Response.ok(response).build();
}
答案 0 :(得分:0)
使用"instructor"
运算符代替$in
,如下所示:
$all
BasicDBObject query = new BasicDBObject("categories", new BasicDBObject("$in", Arrays.asList(category)));
表示$all
必须包含查询中指定的所有值才能匹配的文档。