SpringBoot整合ElasticSearch

本文记录了在SpringBoot中整合ElasticSearch时遇到的问题,包括因错误配置响应Content-Type导致的HTTP 400 Bad Request错误,以及Docker中Elasticsearch容器自动关闭的解决方案。此外,还介绍了项目的模块配置、属性文件设置、Pojo类、ElasticsearchRepository接口以及测试代码和启动类的编写过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、先记录一下自己傻逼导致的错误,耗费了很长时间才解决

java.io.IOException: Request PUT http://47.107.74.116:9200/yao/news/1 HTTP/1.1 yielded text/plain; charset=UTF-8, should be json: HTTP/1.1 400 Bad Request
	at io.searchbox.client.http.JestHttpClient.deserializeResponse(JestHttpClient.java:207)
	at io.searchbox.client.http.JestHttpClient.execute(JestHttpClient.java:68)
	at io.searchbox.client.http.JestHttpClient.execute(JestHttpClient.java:60)
	at com.dxy.es.controller.EsController.insertIndex(EsController.java:27)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)

测试Jest操作ES,报HTTP/1.1 yielded text/plain; charset=UTF-8, should be json: HTTP/1.1 400 Bad Request错,说是因为response的content-type是 text/plain; charset=UTF-8而不是需要的application/json; charset=UTF-8,因此抛出异常。

最后发现是如下的配置导致的,为啥我要配置下面那两行呢,是因为第一行配置显示已过期,我就想着有没有其他配置替代,源码里面就找到了后面两行,抱着试一试的心态加上了,后来忘了去掉就造成了这么傻逼的错误

spring.elasticsearch.jest.uris=http://47.107.74.116:9200
##下面两行配置启用了proxy代理,导致上面的错误发生
#spring.elasticsearch.jest.proxy.host=47.107.74.116
#spring.elasticsearch.jest.proxy.port=9200
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值