1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Spring Framework RestTemplateをElasticsearchのクライアントとする場合の注意

Last updated at Posted at 2019-12-20

ElasticsearchのクライアントとしてSpring Framework RestTemplateを使用する際に引っかかったので以下の点に注意です。

HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("application", "json", Charset.forName("utf-8")));
String q = "{\"query\":" + [クエリ] +"}";
HttpEntity<String> entity = new HttpEntity<String>(q, headers);
HttpEntity<String> result = restTemplate.exchange([URL], HttpMethod.POST, entity, String.class);

以下のようにHttpEntityでString型を指定した場合

HttpEntity<String> entity = new HttpEntity<String>(q, headers);

以下のようにヘッダにContentsTypeを指定しなければElasticsearchがエラーとしてHTTPステータスコード406 Not Acceptableを返却します。

headers.setContentType(new MediaType("application", "json", Charset.forName("utf-8")));
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?