0

我正在尝试返回 SOLRDocList 类型的 JSON,即:

{numFound=0,start=0,docs=[]}{"start":0,"empty":true,"class":"class org.apache.solr.common.SolrDocumentList","numFound":0}

但是,由于我将 Service 类用作:

@POST
@Path("/userQuery")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
public SolrDocumentList userQuery(String p){  
  int sizy;
  String stry;
  StringBuilder sb = new StringBuilder();
  SolrDocument doc = null;
  SolrDocumentList docList = null;

  List<String> arr = new ArrayList();
  StringTokenizer token = new StringTokenizer(p,",");
  while(token.hasMoreElements()){
      stry = token.nextToken();
      arr.add(stry);
  }

  Set<String> xrr = new HashSet<String>(arr);

      SolrQuery query = new SolrQuery(createQuery(arr));
      query.setIncludeScore(false);
      query.setFields("country_code,last_name,raw_location");
      query.setParam("wt", "json");
      System.out.println(query);
      QueryResponse qr = authorSearcher.query(query);
      docList = qr.getResults();

  return docList;
}
Error is :
SEVERE: A message body writer for Java class org.apache.solr.common.SolrDocumentList, and Java type class org.apache.solr.common.SolrDocumentList, and MIME media type application/json was not found
SEVERE: The registered message body writers compatible with the MIME media type are:
application/json ->
  com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
*/* ->

我想知道是否有任何可能的方法可以将 JSON 的 SOLRDocList 类型转换为支持的类型来解决这个问题?谢谢你的时间 !

4

1 回答 1

0

我想通了,因为我使用的是 solr4j ,所以这个库不支持 JSON 作为输出,在这种情况下使用外部库或:

Map<Integer,Object> solrDocMap = new HashMap<Integer,Object>();
              int counter = 1;
              for(Map singleDoc: docList)
              {
                  solrDocMap.put(counter, new JSONObject(singleDoc));
                  counter++;
              }
              try {
                  returnResults.put("docs",solrDocMap);
                    }

当你想返回一个 JSON 字符串而不是使用 3rd party libraries 时,使用类似的东西:

  String x = ""+returnResults;
  return x;
于 2012-12-20T12:43:42.337 回答