使用Jest操作ElasticSearch 报错:No time zone indicator问题的解决方案
生活随笔
收集整理的這篇文章主要介紹了
使用Jest操作ElasticSearch 报错:No time zone indicator问题的解决方案
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
問題描述: 在ElasticSearch中的時(shí)間格式是 yyyy-MM-dd'T'HH:mm:ss 的格式的,但是在使用Jest查詢結(jié)果時(shí) 執(zhí)行jestClient.execute(search) ,會(huì)提示錯(cuò)誤:
com.google.gson.JsonSyntaxException: 2018-09-30T16:07:53 ... Caused by: java.text.ParseException: Failed to parse date ["2018-09-30T16:07:53"]: No time zone indicatorat com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:274) ~[gson-2.8.5.jar:na]at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:149) ~[gson-2.8.5.jar:na]... 48 common frames omitted可以看到錯(cuò)誤時(shí)Gson的錯(cuò)誤,我們數(shù)據(jù)的格式和Jest去查詢的格式不相符所致
看源碼在io.searchbox.client.AbstractJestClient#ELASTIC_SEARCH_DATE_FORMAT 中定義了 public static final String ELASTIC_SEARCH_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ"; 與我們的數(shù)據(jù)格式不相符,因此需要去定義Gson的格式
解決方案: 將JestClient自己實(shí)現(xiàn)并加入spring的管理中,問題得到解決
/*** @author qi.liu* @create 2018-10-18 15:14* @desc 描述:**/ @Configuration public class JestClientConfig {@Autowired(required = false)private JestClientFactory jestClientFactory;@Autowiredprivate ElasticsearchJestProperties properties;@Beanpublic JestClient createJestClient() {GsonFactoryBean gsonFactoryBean = new GsonFactoryBean();gsonFactoryBean.setDateFormatPattern("yyyy-MM-dd'T'HH:mm:ss");gsonFactoryBean.afterPropertiesSet();HttpClientConfig.Builder builder = new HttpClientConfig.Builder(properties.getUri()).gson(gsonFactoryBean.getObject()).maxTotalConnection(properties.getMaxTotalConnection()).defaultMaxTotalConnectionPerRoute(properties.getDefaultMaxTotalConnectionPerRoute()).readTimeout(properties.getReadTimeout()).multiThreaded(properties.getMultiThreaded());JestClientFactory factory = jestClientFactory != null ? jestClientFactory : new JestClientFactory();factory.setHttpClientConfig(builder.build());return factory.getObject();} }時(shí)間被正確解析出來:
總結(jié)
以上是生活随笔為你收集整理的使用Jest操作ElasticSearch 报错:No time zone indicator问题的解决方案的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OSError:[Errno 13] P
- 下一篇: Spring MVC注解故障追踪记