ResTemplate的几种请求方式


本文为转载文,原文地址请戳这这这里.

GET请求

在RestTemplate中,发送一个GET请求,具体方法如下:

1. getForEntity

getForEntity方法的返回值是一个ResponseEntity<T>,是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();
}
  • 方法的第一个参数表示要调用的服务地址;
  • 方法的第二个参数表示返回的消息体的数据类型;
  • 方法的第三个参数表示传给调用的方法的参数。

显示结果如下:
在这里插入图片描述

如果需要传递参数,可以用以下两种方式:

@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();
}
  • 可以用一个数字做占位符,最后是一个可变长度的参数,来一一替换前面的占位符
  • 也可以前面使用name={name}这种形式,最后一个参数是一个map,map的key即为前边占位符的名字,map的value为参数值

第一个调用地址也可以是一个URI而不是字符串,这个时候我们通过Spring中提供的 UriComponents 来构建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();
}

2. getForObject

getForObject函数实际上是对getForEntity函数的进一步封装,如果你只关注返回的内容,对其他信息都不关注,此时可以使用getForObject。 相当于getForEntity(),再getBody()

POST请求

1. postForEntity

用法和getForEntity一致,传参位置不同,第二个参数为传给调用方法的参数。

2. postForObject

用法和getForObject一致,传参位置不同,第二个参数为传给调用方法的参数。

3. postForLocation

postForLocation也是提交新资源,提交成功之后,返回新资源的URI,postForLocation的参数和前面两种的参数基本一致,只不过该方法的返回值为Uri,这个只需要服务提供者返回一个Uri即可,该Uri表示新资源的位置。

PUT请求

put方法的参数和前面介绍的postForEntity方法的参数基本一致,只是put方法没有返回值而已。

@RequestMapping("/put")
public void put() {
    Book book = new Book();
    book.setName("红楼梦");
    restTemplate.put("http://HELLO-SERVICE/getbook3/{1}", book, 99);
}
  • 第二个参数 book对象 是我要提交的参数
  • 第三个参数 99 用来替换前面的占位符{1}

DELETE请求

也没有返回值

@RequestMapping("/delete")
public void delete() {
    restTemplate.delete("http://HELLO-SERVICE/getbook4/{1}", 100);
}

注意点:

  1. 本文示例返回参数为String,当然实际使用中也可以是一个类,比如People,那就在方法里写 People.class;
ResponseEntity<People> responseEntity = restTemplate.postForEntity(uri, People.class);
  1. 传参时,调用的时候如果使用GET和POST,参数位置是不同的;
ResponseEntity<PackScanRecVo> responseEntity = restTemplate.getForEntity(url, PackScanRecVo.class, "传参");
ResponseEntity<PackScanRecVo> responseEntity = restTemplate.postForEntity(url, "传参", PackScanRecVo.class);

猜你喜欢

转载自blog.csdn.net/weixin_43525116/article/details/84955035