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;

}

posted @   技术小白丁  阅读(34362)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
点击右上角即可分享
微信分享提示