【发布时间】:2018-01-16 00:28:41
【问题描述】:
我是 Solr 的新手。我想使用 Java 连接到我的 solr 核心并以 XML 格式返回结果。通过参考官方文档,我可以得到二进制形式的结果。以下是我的代码:
public static void main(String[] args) throws SolrServerException, IOException {
String urlString = "http://localhost:8983/solr/index1/";
SolrClient solr = new HttpSolrClient.Builder(urlString).build();
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
QueryResponse response = solr.query(query);
System.out.println(response.toString());
}
我还尝试研究如何获得响应。我发现这个链接说“如果你想获得原始的 xml 响应,只需选择任何 java HTTP 客户端,构建请求并将其发送到 Solr。你会得到一个很好的 XML 字符串..”solr response in xml format 我编写了下面的代码,但它没有给我响应
public static void main(String[] args) throws ClientProtocolException, IOException
{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://localhost:8983/solr/index1/select?q=*:*&wt=xml");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
System.out.println(response1);
}
}
输出:
HttpResponseProxy{HTTP/1.1 200 OK [Content-Type: application/xml; charset=UTF-8, Transfer-Encoding: chunked] ResponseEntityProxy{[Content-Type: application/xml; charset=UTF-8,Chunked: true]}}
在官网https://lucene.apache.org/solr/guide/6_6/using-solrj.html,提到使用
solr.setParser(new XMLResponseParser());
获取 XML 响应,但我不确定如何使用它,因为没有给出任何示例。任何帮助表示赞赏。
编辑: 正如约翰的评论中提到的,我已将我的代码修改为:
System.out.println(EntityUtils.toString(response1.getEntity()));
【问题讨论】:
-
response1.getEntity().getContent() 的输出是什么?
-
它给了我输出:org.apache.http.conn.EofSensorInputStream@6f7fd0e6
-
是的,因为它是输入流,这个 EntityUtils.toString(response1.getEntity()) 怎么样
-
成功了。我能够看到输出,但不确定为什么它还在 XML 输出之前打印一些 javascript
-
你应该检查一下,可能是因为它发送了一个 html 页面或什么?