SpringCloud00 _Restemplate的getForEntity、getForObject、 postForEntity、postForObject(上)

简介: ①. getForEntity②. getForObject

①. getForEntity


①. getForEntity方法的返回值是一个ResponseEntity,ResponseEntity是Spring对HTTP请求响应的封装,包括了几个重要的元素,如响应码、contentType、contentLength、响应消息体等。比如下面一个例子:


@RequestMapping("/gethello")
public String getHello() {
    ResponseEntity<String> 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("<hr>")
            .append("responseEntity.getStatusCode():").append(statusCode).append("<hr>")
            .append("responseEntity.getStatusCodeValue():").append(statusCodeValue).append("<hr>")
            .append("responseEntity.getHeaders():").append(headers).append("<hr>");
    return result.toString();
}


微信图片_20220106204238.png


②. 关于这段代码,我说如下几点:


getForEntity的第一个参数为我要调用的服务的地址,这里我调用了服务提供者提供的/hello接口,注意这里是通过服务名调用而不是服务地址,如果写成服务地址就没法实现客户端负载均衡了


getForEntity第二个参数String.class表示我希望返回的body类型是String


③. 最终显示结果如下:


微信图片_20220106204257.png


④. 有时候我在调用服务提供者提供的接口时,可能需要传递参数,有两种不同的方式,如下


可以用一个数字做占位符,最后是一个可变长度的参数,来一一替换前面的占位符


也可以前面使用name={name}这种形式,最后一个参数是一个map,map的key即为前边占


位符的名字,map的value为参数值


@RequestMapping("/sayhello")
public String sayHello() {
    ResponseEntity<String> responseEntity = restTemplate.getForEntity
    ("http://HELLO-SERVICE/sayhello?name={1}", String.class, "张三");
    return responseEntity.getBody();
}
@RequestMapping("/sayhello2")
public String sayHello2() {
    Map<String, String> map = new HashMap<>();
    map.put("name", "李四");
    ResponseEntity<String> responseEntity = restTemplate.getForEntity
    ("http://HELLO-SERVICE/sayhello?name={name}", String.class, map);
    return responseEntity.getBody();
}


⑤. 第一个调用地址也可以是一个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<String> responseEntity = restTemplate.getForEntity(uri, String.class);
    return responseEntity.getBody();
}


②. getForObject


①. getForObject函数实际上是对getForEntity函数的进一步封装,如果你只关注返回的消息体的内容,对其他信息都不关注,此时可以使用getForObject,举一个简单的例子,如下:


  @RequestMapping("/book2")
  public Book book2() {
      Book book = restTemplate.getForObject
      ("http://HELLO-SERVICE/getbook1", Book.class);
      return book;
  }


微信图片_20220106204428.png

微信图片_20220106204452.png



②. 这几个重载方法参数的含义和getForEntity一致,我就不再赘述了


相关文章
|
2月前
|
Java Nacos Sentinel
Spring Cloud Alibaba 面试题及答案整理,最新面试题
Spring Cloud Alibaba 面试题及答案整理,最新面试题
351 0
|
28天前
|
人工智能 Java Spring
使用 Spring Cloud Alibaba AI 构建 RAG 应用
本文介绍了RAG(Retrieval Augmented Generation)技术,它结合了检索和生成模型以提供更准确的AI响应。示例中,数据集(包含啤酒信息)被加载到Redis矢量数据库,Spring Cloud Alibaba AI Starter用于构建一个Spring项目,演示如何在接收到用户查询时检索相关文档并生成回答。代码示例展示了数据加载到Redis以及RAG应用的工作流程,用户可以通过Web API接口进行交互。
52343 62
|
11天前
|
监控 Java 应用服务中间件
替代 Hystrix,Spring Cloud Alibaba Sentinel 快速入门
替代 Hystrix,Spring Cloud Alibaba Sentinel 快速入门
|
27天前
|
消息中间件 Java 持续交付
Spring Cloud Alibaba 项目搭建步骤和注意事项
Spring Cloud Alibaba 项目搭建步骤和注意事项
206 0
Spring Cloud Alibaba 项目搭建步骤和注意事项
|
25天前
|
存储 SpringCloudAlibaba 关系型数据库
springcloud alibaba(5)
springcloud alibaba
97 0
|
25天前
|
SpringCloudAlibaba Nacos
springcloud alibaba(4)
springcloud alibaba
125 0
|
25天前
|
SpringCloudAlibaba 容灾 测试技术
springcloud alibaba(3)
springcloud alibaba
68 0
|
25天前
|
SpringCloudAlibaba 负载均衡 前端开发
springcloud alibaba(2)
springcloud alibaba
80 0
|
25天前
|
存储 SpringCloudAlibaba Java
springcloud alibaba(1)
springcloud alibaba
52 0

热门文章

最新文章