NamedList <Object>引发类强制转换异常

时间:2019-09-13 10:23:29

标签: java collections

我要这样形成一个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);
            } else if ("grouped".equals(n)) {
                this._groupedInfo = (NamedList)res.getVal(i);
                this.extractGroupedInfo(this._groupedInfo);
            } else if ("expanded".equals(n)) {
                NamedList map = (NamedList)res.getVal(i);
                this._expandedResults = map.asMap(1);
            } else if ("highlighting".equals(n)) {
                this._highlightingInfo = (NamedList)res.getVal(i);
                this.extractHighlightingInfo(this._highlightingInfo);
            } else if ("spellcheck".equals(n)) {
                this._spellInfo = (NamedList)res.getVal(i);
                this.extractSpellCheckInfo(this._spellInfo);
            } else if ("clusters".equals(n)) {
                this._clusterInfo = (ArrayList)res.getVal(i);
                this.extractClusteringInfo(this._clusterInfo);
            } else if ("suggest".equals(n)) {
                this._suggestInfo = (Map)res.getVal(i);
                this.extractSuggesterInfo(this._suggestInfo);
            } else if ("stats".equals(n)) {
                this._statsInfo = (NamedList)res.getVal(i);
                this.extractStatsInfo(this._statsInfo);
            } else if ("terms".equals(n)) {
                this._termsInfo = (NamedList)res.getVal(i);
                this.extractTermsInfo(this._termsInfo);
            } else if ("moreLikeThis".equals(n)) {
                this._moreLikeThisInfo = (NamedList)res.getVal(i);
            } else if ("nextCursorMark".equals(n)) {
                this._cursorMarkNext = (String)res.getVal(i);
            }
        }

        if (this._facetInfo != null) {
            this.extractFacetInfo(this._facetInfo);
        }

    }


1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案,希望对大家有帮助。我从Solr得到了XML格式的响应,并读取了文件并使用XMLResponseParser对其进行了解析。 JsonParser以某种方式不适用于Solar,如果您使用Java反序列化,则Solr中存在不兼容的错误。

这同样适用于查询Response类的内部类型转换。


protected QueryResponse getResponse(String fileName) throws IOException {

        Path path = Paths.get(resDir + "/" + fileName);
        InputStream body= new FileInputStream(path.toFile());
        NamedList<Object> result= processResponse(body, null);
        QueryResponse response = new QueryResponse();
        response.setResponse(result);
        return response;
    }

    private NamedList<Object> processResponse(InputStream body, Object o) {
        XMLResponseParser parser= new XMLResponseParser();
        NamedList<Object> result= parser.processResponse(body, "UTF-8");
        return result;
    }