我正在尝试使用 SolrJ 根据此问题中提供的代码查询我的 solr 数据库,但它不断抛出空指针异常。
我的代码是:
@PUT
@Produces(MediaType.TEXT_PLAIN)
public String returnText(String url) throws MalformedURLException, SolrServerException{
SolrServer server = new HttpSolrServer("http://localhost:8080/apache-solr-1.4.0");
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("?q", "*:*");
params.set("facet", true);
params.set("rows", 5);
QueryResponse response = server.query(params);
System.out.println(response);
return "success";
}
如果我在浏览器中运行这个 url:http://localhost:8080/apache-solr-1.4.0/select/?q=*:*&facet=true&rows=5
那么它会返回正确的值但是如果我输入:http://localhost:8080/apache-solr-1.4.0/select/q=*:*&facet=true&rows=5
那么我会得到与在 eclipse 中运行它时完全相同的错误,错误是:
19/12/2012 1:09:01 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
org.apache.solr.client.solrj.SolrServerException: Server at http://localhost:8080/apache-solr-1.4.0 returned non ok status:500, message:null java.lang.NullPointerException at java.io.StringReader.<init>(StringReader.java:33) at org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:197) at org.apache.solr.search.LuceneQParser.parse(LuceneQParserPlugin.java:78) at org.apache.solr.search.QParser.getQuery(QParser.java:131) at org.apache.solr.handler.component.QueryComponent.prepare(QueryComponent.java:89) at org.apache.solr.handler.component.SearchHandler.handleRequestBody(SearchHandler.java:174) at org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:131) at org.apache.solr.core.SolrCore.execute(SolrCore.java:1316) at org.apache.solr.servlet.SolrDispatchFilter.execute(SolrDispatchFilter.java:338) at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:241) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97) at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57) at com.sun.grizzly.ContextTask.run(ContextTask.java:69) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309) at java.lang.Thread.run(Thread.java:619)
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:328)
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:211)
at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:89)
at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:311)
at geoportal.webservice.download.returnText(download.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
.......
如果我将代码更改为,params.set("q","*:*");
则会收到此错误:
19/12/2012 1:13:30 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
org.apache.solr.client.solrj.SolrServerException: Error executing query
at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:95)
at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:311)
at geoportal.webservice.download.returnText(download.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
.......
关于我做错了什么有什么想法吗?
非常感谢
编辑
这是我基于此的 solrQuery 的新代码:
@PUT
@Produces(MediaType.TEXT_PLAIN)
public String returnText(String url) throws MalformedURLException, SolrServerException{
SolrServer server = new HttpSolrServer("http://localhost:8080/apache-solr-1.4.0");
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.setFacet(true);
query.set("wt", "json");
query.setRows(5);
query.setStart(0);
QueryResponse response = server.query(query);
System.out.println(response);
return "success";
}
但它仍然无法正常工作:(控制台输出:
19/12/2012 2:24:33 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
org.apache.solr.client.solrj.SolrServerException: Error executing query
at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:95)
at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:311)
at geoportal.webservice.download.returnText(download.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
......
编辑
我在我的代码中放了一个System.out.println(query.toString());
,它输出:
q=*%3A*&facet=true&wt=json&rows=5&start=0
如果我在我的浏览器中输入这个
http://localhost:8080/apache-solr-1.4.0/select/q=*%3A*&facet=true&wt=json&rows=5&start=0
然后它再次抛出一个空指针异常。?
但是,如果我在前面手动输入 a ,q=*%3A*...
它就可以工作。所以我假设我的 solr 服务器需要这个?
(这是因为它太旧了吗?),有没有办法硬编码一个?
谢谢