IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> RestTemplate 进行HTTP调用 -> 正文阅读

[网络协议]RestTemplate 进行HTTP调用

参考资料:


概述:

RestTemplate是一种模板语言,?可支持多种 Http Client的http的访问

  • 基于 JDK HttpURLConnection 的 SimpleClientHttpRequestFactory,默认。
  • 基于 Apache HttpComponents Client 的 HttpComponentsClientHttpRequestFactory
  • 基于 OkHttp3的OkHttp3ClientHttpRequestFactory。
  • 基于 Netty4 的 Netty4ClientHttpRequestFactory。

其中HttpURLConnection 和 HttpClient 为原生的网络访问类,OkHttp3采用了 OkHttp3的框架,Netty4 采用了Netty框架。


SpringBoot整合:

  1. 如果使用默认的引擎,就添加配置类
@Configuration
public class RestConifg {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
  1. ?如果使用指定的引擎,如OkHttp3? ,则需要修改配置类为
@Configuration
public class RestConifg {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
    }
}
  1. 使用的时候可以? @autowire? 来进行调用?
    @Autowired
    private RestTemplate restTemplate;
  1. 下面是 getForObject/getForEntity ? postForObject/postForEntity ?exchange 对于get/post方式的请求
  • getForObject/getForEntity请求get路径
    @RequestMapping("/testRestForGet")
    public String testRestForGet(){

        //IP可以换成任意IP
        String url1 = "http://127.0.0.1:8099/getUrl1/100000";
        String url2 = "http://127.0.0.1:8099/getUrl2";

        String result = restTemplate.getForObject(url1, String.class);
        ResponseEntity<String> entity = restTemplate.getForEntity(url1, String.class);
        //参数
        Map<String,Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("pwd","123");
        map.put("age",15);
        User user = restTemplate.getForObject(url2+"?name={name}&pwd={pwd}&age={age}", User.class, map);
        ResponseEntity<User> forEntity = restTemplate.getForEntity(url2+"?name={name}&pwd={pwd}&age={age}", User.class, map);
        log.info(result);
        log.info(entity.toString());
        log.info(entity.getBody().toString());
        log.info(user.toString());
        log.info(forEntity.toString());
        log.info(forEntity.getBody().toString());
        return "finish";
    }
  • ?postForObject/postForEntity请求post请求
    @RequestMapping("/testRestForPost")
    public String testRestForPost(){

        //IP可以换成任意IP
        String url1 = "http://127.0.0.1:8099/postUrl1";
        String url2 = "http://127.0.0.1:8099/postUrl2";


        //post方法必须使用  MultiValueMap 传参  对于单个参数  也可以使用实体但是不能使用单个变量传参  也可以
        MultiValueMap<String,Object> paramMap2 = new LinkedMultiValueMap<>();
        String userName = "prter";
        paramMap2.add("userName",userName);
        paramMap2.add("age",15);

        User u = new User();
        u.setName("王丹丹");
        u.setPwd("123456");
        u.setAge(15);

        User user1 = restTemplate.postForObject(url1, u, User.class);
        ResponseEntity<User> entity1 = restTemplate.postForEntity(url1, u, User.class);


        User user = restTemplate.postForObject(url2, paramMap2, User.class);
        ResponseEntity<User> entity = restTemplate.postForEntity(url2, paramMap2, User.class);

        log.info(user.toString());
        log.info(entity.toString());
        log.info(entity.getBody().toString());
        log.info(user1.toString());
        log.info(entity1.toString());
        log.info(entity1.getBody().toString());
        return "finish";
    }
  • ?postForObject/postForEntity? ?对于
    对于请求的方法  @requestBody和@requestParam  混用的情况  则需要另一种请求方式

    //对于请求的方法  @requestBody和@requestParam  混用的情况  则需要另一种请求方式
    @RequestMapping("/testRestForPost1")
    public String testRestForPost1(){

        //IP可以换成任意IP
        String url = "http://127.0.0.1:8099/postUrl3";

        //申明一个请求头
        HttpHeaders headers = new HttpHeaders();
        //application/json
        headers.setContentType(MediaType.APPLICATION_JSON);

        Map<String,Object> map = new HashMap<>();
        map.put("userName","prter");

        User u = new User();
        u.setName("lilei");
        u.setPwd("123");
        u.setAge(15);
        HttpEntity<User> entityParam = new HttpEntity<User>(u,headers);
        User user = restTemplate.postForObject(url+"?userName={userName}", entityParam, User.class,map);
        log.info(user.toString());

        return "finish";
    }
  • ?exchange? 对于get请求
    //Exchange   既可以进行get访问  也可以进行post访问  并且返回值必须用ResponseEntity包装
    @RequestMapping("/testRestForExchangeGet")
    public String testRestForExchangeGet(){
        String url = "http://127.0.0.1:8099/getUrl2";
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("name","粉红豹");
        paramMap.put("pwd","123");
        paramMap.put("age",15);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        //HttpEntity  包装了传参
        HttpEntity<MultiValueMap<String,Object>> requestEntity = new HttpEntity<>(null,headers);
        //ResponseEntity 包装返回结果
        ResponseEntity<User> response = restTemplate.exchange(url+"?name={name}&pwd={pwd}&age={age}", HttpMethod.GET,requestEntity,User.class,paramMap);
        log.info(response.toString());
        log.info(response.getBody().toString());
        return "finish";
    }
  • ?exchange? 对于post请求
    //Exchange   既可以进行get访问  也可以进行post访问  并且返回值必须用ResponseEntity包装
    @RequestMapping("/testRestForExchangePost")
    public String testRestForExchangePost(){

        String url = "http://127.0.0.1:8099/postUrl2";
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
        paramMap.add("userName","粉红豹");
        paramMap.add("age",15);
        //HttpEntity  包装了传参
        HttpEntity<MultiValueMap<String,Object>> requestEntity = new HttpEntity<>(paramMap);
        //ResponseEntity 包装返回结果
        ResponseEntity<User> response = restTemplate.exchange(url, HttpMethod.POST,requestEntity,User.class);
        log.info(response.toString());
        log.info(response.getBody().toString());
        return "finish";
    }

详细信息请参照参考demo

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-02-28 15:59:55  更:2022-02-28 16:02:30 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年10日历 -2024/10/5 13:27:05-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码