zoukankan      html  css  js  c++  java
  • 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;

    }

  • 相关阅读:
    linux LTIB学习笔记
    wince WaitForMultipleObjects需要注意的问题
    微信小程序在苹果上出现[request:fail 发生了 SSL 错误无法建立与该服务器的安全连接。]错误的解决方案
    Windows 2008之PKI实战4:吊销
    十个不找工作的理由
    [zt]我奋斗了18年不是为了和你一起喝咖啡
    [zt]Java/PHP/C 几种语言 RSA 的互操作
    全职共享和兼职的一些思考pkill
    定价策略(翻译稿)
    Windows 2008之PKI实战1:管理
  • 原文地址:https://www.cnblogs.com/jnba/p/10522554.html
Copyright © 2011-2022 走看看