【RestTemplate】使用RestTemplate访问Elasticsearch示例
【依赖】
一般来说,SpringBoot程序自带RestTemplate,不需要额外引用。
如果是Maven程序,可以载入以下引用:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.3.RELEASE</version> </dependency>
【GET请求】
Code:
import org.springframework.web.client.RestTemplate; public class Test1 { public static void main(String[] args){ RestTemplate restTemplate = new RestTemplate(); String json = restTemplate.getForObject("http://192.168.32.130:9200/",String.class); System.out.println(json); } }
输出:
{ "name" : "node-1", "cluster_name" : "dual-app130", "cluster_uuid" : "OHQmV0p8QUCLutfepnipqQ", "version" : { "number" : "7.11.0", "build_flavor" : "default", "build_type" : "tar", "build_hash" : "8ced7813d6f16d2ef30792e2fcde3e755795ee04", "build_date" : "2021-02-08T22:44:01.320463Z", "build_snapshot" : false, "lucene_version" : "8.7.0", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" }
【POST请求】
code:
import org.springframework.web.client.RestTemplate; import java.util.LinkedHashMap; import java.util.Map; public class Test1 { public static void main(String[] args){ RestTemplate restTemplate = new RestTemplate(); Map<String,Object> paramMap=new LinkedHashMap<String,Object>(); paramMap.put("name", "hy"); paramMap.put("age", "25"); String rsps=restTemplate.postForObject("http://192.168.32.130:9200/moft/emp/111?pretty",paramMap,String.class); System.out.println(rsps); } }
输出:
{ "_index" : "moft", "_type" : "emp", "_id" : "111", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 }
查询刚才插入的数据:
RestTemplate restTemplate = new RestTemplate(); String json = restTemplate.getForObject("http://192.168.32.130:9200/moft/emp/111?pretty",String.class); System.out.println(json);
输出:
{ "_index" : "moft", "_type" : "emp", "_id" : "111", "_version" : 1, "_seq_no" : 0, "_primary_term" : 1, "found" : true, "_source" : { "name" : "hy", "age" : "25" } }
【查找emp下所有】
Code:
RestTemplate restTemplate = new RestTemplate(); String json = restTemplate.getForObject("http://192.168.32.130:9200/moft/emp/_search?pretty",String.class); System.out.println(json);
输出:
{ "took" : 9, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "moft", "_type" : "emp", "_id" : "111", "_score" : 1.0, "_source" : { "name" : "hy", "age" : "25" } }, { "_index" : "moft", "_type" : "emp", "_id" : "112", "_score" : 1.0, "_source" : { "name" : "巴菲特", "age" : "27" } }, { "_index" : "moft", "_type" : "emp", "_id" : "113", "_score" : 1.0, "_source" : { "name" : "比尔盖茨", "age" : "29" } } ] } }
【按名称查找】
Code:
RestTemplate restTemplate = new RestTemplate(); String json = restTemplate.getForObject("http://192.168.32.130:9200/moft/emp/_search?q=name:比尔盖茨&pretty",String.class); System.out.println(json);
输出:
{ "took" : 49, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 2.4398782, "hits" : [ { "_index" : "moft", "_type" : "emp", "_id" : "113", "_score" : 2.4398782, "_source" : { "name" : "比尔盖茨", "age" : "29" } } ] } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2020-02-25 Oracle的timestamp字段更新实验 结论:只有逐条更新才能保证timestamp字段有差别,批量更新只会得到一致的时间,此操作无关时间精度.
2015-02-25 【高中数学/极值问题】已知:a,b皆为正实数,且2a+b=1,求:a/(2-2a)+b/(2-b)的最小值?
2014-02-25 【Canvas与光阑】立方体六彩光阑