NamedList<Object> 抛出类转换异常

我想形成一个 NamedList 对象,如下所示:


response={numFound=57279026,start=0,docs=[SolrDocument{timestamp_update=Thu Jan 01 01:00:00 CET 1970}]}}

当我这样做时,当我的代码尝试访问结果时,我会抛出异常:SolrDocumentList results = response.getResults();


**java.lang.ClassCastException: org.apache.solr.common.util.SimpleOrderedMap cannot be cast to org.apache.solr.common.SolrDocumentList**

我应该如何创建 NamedList,以便它不会引发异常


这是我的做法:


NamedList<Object> nl = new SimpleOrderedMap<>();

private static  Map<String, Object> solrDocumentMap= new HashMap<>();

solrDocumentMap.put("timestamp_update", TIMESTAMP_UPDATE);

solrDocument= new SolrDocument(solrDocumentMap);

solrDocumentList.add(solrDocument);


nl.add("numFound", "57279026");

nl.add("start", "0");

 nl.add("docs", solrDocumentList);


 NamedList<Object> nl1 = new NamedList<>(Collections.singletonMap("response", nl));

 response.setResponse(nl1);

这是 QueryResponse 的内置类,它将响应转换为 SolarDocument

  public void setResponse(NamedList<Object> res) {

        super.setResponse(res);


        for(int i = 0; i < res.size(); ++i) {

            String n = res.getName(i);

            if ("responseHeader".equals(n)) {

                this._header = (NamedList)res.getVal(i);

            } else if ("response".equals(n)) {

                this._results = (SolrDocumentList)res.getVal(i);

            } else if ("sort_values".equals(n)) {

                this._sortvalues = (NamedList)res.getVal(i);

            } else if ("facet_counts".equals(n)) {

                this._facetInfo = (NamedList)res.getVal(i);

            } else if ("debug".equals(n)) {

                this._debugInfo = (NamedList)res.getVal(i);

                this.extractDebugInfo(this._debugInfo);

            } 


梵蒂冈之花
浏览 103回答 1
1回答

牛魔王的故事

终于找到解决办法了,希望对大家有帮助。我从 Solr 获得了 XML 格式的响应,并读取了该文件并使用 XMLResponseParser 对其进行了解析。不知何故,JsonParser 不适用于 Solar,如果您使用 java 反序列化,Solr 中存在一个不兼容的错误。这也适用于查询响应类的内部类型转换。protected QueryResponse getResponse(String fileName) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; Path path = Paths.get(resDir + "/" + fileName);&nbsp; &nbsp; &nbsp; &nbsp; InputStream body= new FileInputStream(path.toFile());&nbsp; &nbsp; &nbsp; &nbsp; NamedList<Object> result= processResponse(body, null);&nbsp; &nbsp; &nbsp; &nbsp; QueryResponse response = new QueryResponse();&nbsp; &nbsp; &nbsp; &nbsp; response.setResponse(result);&nbsp; &nbsp; &nbsp; &nbsp; return response;&nbsp; &nbsp; }&nbsp; &nbsp; private NamedList<Object> processResponse(InputStream body, Object o) {&nbsp; &nbsp; &nbsp; &nbsp; XMLResponseParser parser= new XMLResponseParser();&nbsp; &nbsp; &nbsp; &nbsp; NamedList<Object> result= parser.processResponse(body, "UTF-8");&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java