学习Solr写Demo时,服务器报错
后端代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-solr.xml")
public class TestTemplate {
@Autowired
private SolrTemplate solrTemplate;
@Test
public void testAdd() {
TbItem item = new TbItem();
// 主键相同,即修改
item.setId(1L);
item.setBrand("小米儿");
item.setCategory("手机");
item.setGoodsId(1L);
item.setSeller("谁**买小米儿");
item.setTitle("新鲜的小米儿只要998");
item.setPrice(new BigDecimal(998));
solrTemplate.saveBean(item);
solrTemplate.commit();
}
}
错误详情:
org.springframework.data.solr.UncategorizedSolrException: Expected mime type application/octet-stream but got text/html.
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 405 HTTP method POST is not supported by this URL</title>
</head>
<body><h2>HTTP ERROR 405</h2>
<p>Problem accessing /solr/index.html. Reason:
<pre> HTTP method POST is not supported by this URL</pre></p>
</body>
</html>
; nested exception is org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Expected mime type application/octet-stream but got text/html.
解决方法:
修改配置文件spring-solr.xml,url地址里的 /#
删掉
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:solr="http://www.springframework.org/schema/data/solr"
xsi:schemaLocation="http://www.springframework.org/schema/data/solr
http://www.springframework.org/schema/data/solr/spring-solr.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 错误的solr服务器地址 -->
<!-- <solr:solr-server id="solrServer"
url="http://192.168.188.102:8983/solr/#/collection1" /> -->
<!-- 正确的solr服务器地址 -->
<solr:solr-server id="solrServer"
url="http://192.168.188.102:8983/solr/collection1" />
<!-- solr模板,使用solr模板可对索引库进行CRUD操作 -->
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
</beans>