Spring RestTemplate 之get请求

一,简介:Spring RestTemplate 是 Spring 提供的用于访问 Rest 服务的客户端,RestTemplate 提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率

二、RestTemplate中几种常见请求方法的使用

    ●get请求:在RestTemplate中,发送一个GET请求,我们可以通过如下两种方式

         第一种:getForEntity

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

 

@Controller

@RequestMapping("/restTest")

public class RestTempLateTest {

private RestTemplate restTemplate = new RestTemplate();

@RequestMapping("/hello")

@ResponseBody

public String getHello() {

// ResponseEntity<IntMonitor> res = restTemplate.getForEntity(url,

// IntMonitor)

ResponseEntity<String> res = restTemplate.getForEntity(

"http://10.145.198.143:8081/ords/data_service/monitor/IntMonitor",

String.class);

String body = res.getBody();

return body;

}

}

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

        @RequestMapping("/hello1/{flag}")

@ResponseBody

public String getHello1(@PathVariable String flag){

ResponseEntity<String> res = restTemplate.getForEntity(

"http://10.145.198.143:8081/ords/data_service/monitor/IntMonitor/{1}", 

String.class, 

"1");

String body = res.getBody();

return body;

}

 

        @RequestMapping("/hello2/{flag}")

@ResponseBody

public String getHello2(@PathVariable String flag){

Map<String, Object> map = new HashMap<String, Object>();

map.put("flag", flag);

ResponseEntity<String> res = restTemplate.getForEntity(

"http://10.145.198.143:8081/ords/data_service/monitor/IntMonitor/{flag}", 

String.class, 

map);

String body = res.getBody();

return body;

}

    

        @RequestMapping("/hello3/{flag}")

@ResponseBody

public String getHello3(@PathVariable String flag) {

UriComponents uriComponents = UriComponentsBuilder

.fromUriString(

"http://10.145.198.143:8081/ords/data_service/monitor/IntMonitor/{flag}")

.build().expand(flag);

URI uri = uriComponents.toUri();

ResponseEntity<String>  res = restTemplate.getForEntity(uri, String.class);

String body = res.getBody();

return body;

}

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

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

  • 调用地址也可以是一个url,而不是一个字符串,这样可以直接调用url.

        第二种:getForObject

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

            举一个简单的例子,如下:

        @RequestMapping("/hello4/{flag}")

@ResponseBody

public String getHello4(@PathVariable String flag) {

UriComponents uriComponents = UriComponentsBuilder

.fromUriString(

"http://10.145.198.143:8081/ords/data_service/monitor/IntMonitor/{flag}")

.build().expand(flag);

URI uri = uriComponents.toUri();

String  res = restTemplate.getForObject(uri, String.class);

return res;

}

转载于:https://www.cnblogs.com/jnba/p/10522554.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值