预期的mime类型application / xml但是得到了text / html

时间:2015-01-05 14:11:53

标签: java solr mime

我在eclipse中尝试solrj但是我收到了这条错误消息:

org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Expected mime type application/xml but got text/html
    at org.apache.solr.client.solrj.impl.HttpSolrServer.executeMethod(HttpSolrServer.java:512)
    at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:210)
    at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:206)
    at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:91)
    at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:301)
    at pack.SearchServlet.service(SearchServlet.java:55)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

以下是我的代码:

queryString=request.getParameter("query");
keywords=queryString.split(" ");

server=new HttpSolrServer(url);
server.setMaxRetries(1);
server.setConnectionTimeout(5000);
server.setParser(new XMLResponseParser());
server.setSoTimeout(1000);
server.setDefaultMaxConnectionsPerHost(100);
server.setMaxTotalConnections(100);
server.setFollowRedirects(false);
server.setAllowCompression(true);

query=new SolrQuery();
query.setQuery("title:"+keywords[0]+" OR content"+keywords[0]);
query.setStart(0);
query.setRows(8);

query.setHighlight(true);
query.addHighlightField("content");
query.addHighlightField("title");

queryResponse=server.query(query);

3 个答案:

答案 0 :(得分:1)

我通过扩展XMLResponseParser,覆盖getContentType并将其用作响应解析器来解决此问题。

protected class QESXMLResponseParser extends XMLResponseParser {
    public QESXMLResponseParser() {
        super();
    }

    @Override
    public String getContentType() {
        return "text/xml; charset=UTF-8";
    }
}

然后,只更改您的代码以使用“新解析器”

server.setConnectionTimeout(5000);
server.setParser(new QESXMLResponseParser());
server.setSoTimeout(1000);

答案 1 :(得分:0)

我猜你正在使用一个相当现代的solrj与一个相当老的solr交谈。 This bug使用过于严格的mime类型比较来引用该问题。

最简单(但可能不是最好的)解决方案可能会降级您的solrj文件。我不确切知道问题什么时候出现,但我知道我的东西适用于solr-solrj-4.3.1.jar运行solr 1.4安装。

答案 2 :(得分:-1)

查看Solr服务器控制台或日志文件。很可能你的URL错误并且返回HTML错误页面而不是Solr响应。服务器日志中应该存在特定错误。