一、先记录一下自己傻逼导致的错误,耗费了很长时间才解决
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