文章目录

  • ①. getForEntity
  • ②. getForObject
  • ③. postForEntity
  • ④. postForObject


①. 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();
}

resttemplate的postforobject测试 resttemplate getforentity_封装

  • ②. 关于这段代码,我说如下几点:
  1. getForEntity的第一个参数为我要调用的服务的地址,这里我调用了服务提供者提供的/hello接口,注意这里是通过服务名调用而不是服务地址,如果写成服务地址就没法实现客户端负载均衡了
  2. getForEntity第二个参数String.class表示我希望返回的body类型是String
  • ③. 最终显示结果如下:

resttemplate的postforobject测试 resttemplate getforentity_封装_02

  • ④. 有时候我在调用服务提供者提供的接口时,可能需要传递参数,有两种不同的方式,如下
  1. 可以用一个数字做占位符,最后是一个可变长度的参数,来一一替换前面的占位符
  2. 也可以前面使用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;
	}

resttemplate的postforobject测试 resttemplate getforentity_封装_03

resttemplate的postforobject测试 resttemplate getforentity_封装_04

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

③. postForEntity

  • ①. 该方法和get请求中的getForEntity方法类似,如下例子:
  1. 方法的第一参数表示要调用的服务的地址
  2. 方法的第二个参数表示上传的参数
  3. 方法的第三个参数表示返回的消息体的数据类型
@RequestMapping("/book3")
	public Book book3() {
	    Book book = new Book();
	    book.setName("红楼梦");
	    ResponseEntity<Book> responseEntity = restTemplate.postForEntity
	    ("http://HELLO-SERVICE/getbook2", book, Book.class);
	    return responseEntity.getBody();
	}
  • ②. 这里创建了一个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;
	}
  • ③. 服务提供者接收到服务消费者传来的参数book,给其他属性设置上值再返回,调用结果如下:

resttemplate的postforobject测试 resttemplate getforentity_占位符_05

④. postForObject

  • ①. 如果你只关注,返回的消息体,可以直接使用postForObject。用法和getForObject一致