---
title: "SpringCloud00 _Restemplate的getForEntity、getForObject、 postForEntity、postForObjec"
author: "arpenker"
date: "2022-10-20T02:26:16Z"
tags: []
categories: ["编程语言"]
slug: "springcloud00-restemplate-getforentity-getforobject-postforentity-postforobjec"
draft: false
lastmod: "2022-10-20T02:26:16Z"
id: 2538635
url: "https://arpenker.iteye.com/blog/2538635"
---

SpringCloud00 \_Restemplate的getForEntity、getForObject、 postForEntity、postForObject 所得皆惊喜 于 2020-12-08 10:49:55 发布 2399 收藏 9 分类专栏： SpringCloud微服务 版权 SpringCloud微服务 专栏收录该内容 9 篇文章15 订阅 订阅专栏 文章目录 ①. getForEntity ②. getForObject ③. postForEntity ④. postForObject ①. getForEntity ①. getForEntity方法的返回值是一个ResponseEntity,ResponseEntity是Spring对HTTP请求响应的封装,包括了几个重要的元素,如响应码、contentType、contentLength、响应消息体等。比如下面一个例子: @RequestMapping("/gethello") public String getHello() { ResponseEntity responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class); String body = responseEntity.getBody();//获取到相应体 HttpStatus statusCode = responseEntity.getStatusCode();//获取到状态码 int statusCodeValue = responseEntity.getStatusCodeValue(); HttpHeaders headers = responseEntity.getHeaders();//获取到头信息 StringBuffer result = new StringBuffer(); result.append("responseEntity.getBody():").append(body).append("

* * *

") .append("responseEntity.getStatusCode():").append(statusCode).append("

* * *

") .append("responseEntity.getStatusCodeValue():").append(statusCodeValue).append("

* * *

") .append("responseEntity.getHeaders():").append(headers).append("

* * *

"); return result.toString(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ②. 关于这段代码,我说如下几点: getForEntity的第一个参数为我要调用的服务的地址,这里我调用了服务提供者提供的/hello接口,注意这里是通过服务名调用而不是服务地址,如果写成服务地址就没法实现客户端负载均衡了 getForEntity第二个参数String.class表示我希望返回的body类型是String ③. 最终显示结果如下: ④. 有时候我在调用服务提供者提供的接口时,可能需要传递参数,有两种不同的方式,如下 可以用一个数字做占位符,最后是一个可变长度的参数,来一一替换前面的占位符 也可以前面使用name={name}这种形式,最后一个参数是一个map,map的key即为前边占位符的名字,map的value为参数值 @RequestMapping("/sayhello") public String sayHello() { ResponseEntity responseEntity = restTemplate.getForEntity ("http://HELLO-SERVICE/sayhello?name={1}", String.class, "张三"); return responseEntity.getBody(); } @RequestMapping("/sayhello2") public String sayHello2() { Map map = new HashMap&lt;&gt;(); map.put("name", "李四"); ResponseEntity responseEntity = restTemplate.getForEntity ("http://HELLO-SERVICE/sayhello?name={name}", String.class, map); return responseEntity.getBody(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ⑤. 第一个调用地址也可以是一个URI而不是字符串,这个时候我们构建一个URI即可,参数神马的都包含在URI中了,如下: @RequestMapping("/sayhello3") public String sayHello3() { UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://HELLO-SERVICE/sayhello?name={name}").build().expand("王五").encode(); URI uri = uriComponents.toUri(); ResponseEntity responseEntity = restTemplate.getForEntity(uri, String.class); return responseEntity.getBody(); } 1 2 3 4 5 6 7 ②. getForObject ①. getForObject函数实际上是对getForEntity函数的进一步封装,如果你只关注返回的消息体的内容,对其他信息都不关注,此时可以使用getForObject,举一个简单的例子,如下: @RequestMapping("/book2") public Book book2() { Book book = restTemplate.getForObject ("http://HELLO-SERVICE/getbook1", Book.class); return book; } 1 2 3 4 5 6 ②. 这几个重载方法参数的含义和getForEntity一致,我就不再赘述了 ③. postForEntity ①. 该方法和get请求中的getForEntity方法类似,如下例子: 方法的第一参数表示要调用的服务的地址 方法的第二个参数表示上传的参数 方法的第三个参数表示返回的消息体的数据类型 @RequestMapping("/book3") public Book book3() { Book book = new Book(); book.setName("红楼梦"); ResponseEntity responseEntity = restTemplate.postForEntity ("http://HELLO-SERVICE/getbook2", book, Book.class); return responseEntity.getBody(); } 1 2 3 4 5 6 7 8 ②. 这里创建了一个Book对象,这个Book对象只有name属性有值,将之传递到服务提供者那里去,服务提供者代码如下: @RequestMapping(value = "/getbook2", method = RequestMethod.POST) public Book book2(@RequestBody Book book) { System.out.println(book.getName()); book.setPrice(33); book.setAuthor("曹雪芹"); book.setPublisher("人民文学出版社"); return book; } 1 2 3 4 5 6 7 8 ③. 服务提供者接收到服务消费者传来的参数book,给其他属性设置上值再返回,调用结果如下: ④. postForObject ①. 如果你只关注,返回的消息体,可以直接使用postForObject。用法和getForObject一致 ———————————————— 版权声明：本文为CSDN博主「所得皆惊喜」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。 原文链接：https://blog.csdn.net/TZ845195485/article/details/110855465