02、RestTemplate学习笔记
        【摘要】 本节配套案例代码:Gitee仓库、Github仓库所有博客文件目录索引:博客目录索引(持续更新)学习视频:动力节点最新SpringCloud视频教程|最适合自学的springcloud+springcloudAlibabaPS:本章节中部分图片是直接引用学习课程课件,如有侵权,请联系删除。RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便
    
    
    
    @[toc]
前言
所有博客文件目录索引:博客目录索引(持续更新)
学习视频:动力节点最新SpringCloud视频教程|最适合自学的springcloud+springcloudAlibaba
PS:本章节中部分图片是直接引用学习课程课件,如有侵权,请联系删除。
一、RestTemplate
RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。
之前的HTTP开发是用apache的HttpClient开发,代码复杂,还得操心资源回收等。代码很复杂,冗余代码多。
二、快速使用
SpringBoot依赖2.7.1版本:我们只需要导入web启动器即可
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
 
 我们来进行编写三种案例:①GET。②POST JSON。③POST 表单。
package com.changlu.resttemplate.controller;
import com.changlu.resttemplate.domain.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
 * @Description: 测试控制器
 * @Author: changlu
 * @Date: 4:04 PM
 */
@RestController
public class TestController {
    @GetMapping("/testGet")
    public String get(String name) {
        System.out.println(name);
        return "ok";
    }
    /**
     * post传参 两种
     *  json参数的核心  header content-type = application/json;charset=utf-8
     *
     * @return
     */
    @PostMapping("/testPost1")
    public String testPost1(@RequestBody User user) {
        System.out.println(user);
        return "ok";
    }
    /**
     * 接收表单参数
     * <form action=/testPost2
     * <input name=ccc value=ssss
     *  header content-type = application/x-www-form-urlencoded
     * @param user
     * @return
     */
    @PostMapping("/testPost2")
    public String testPost2(User user) {
        System.out.println(user);
        return "ok";
    }
}
 
 
接着我们来使用测试类RestTemplate来进行测试:分别对应三个请求
package com.changlu.resttemplate;
import com.changlu.resttemplate.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
@SpringBootTest
class RestTemplateApplicationTests {
    @Test
    void contextLoads() {
        RestTemplate restTemplate = new RestTemplate();
        //访问百度页面
        String url = "https://www.baidu.com";
        String content = restTemplate.getForObject(url, String.class);
        System.out.println(content);
    }
    @Test
    void testGet() {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8080/testGet?name=cxs";
        //        String result = restTemplate.getForObject(url, String.class);
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        // http:// 协议 (规范 接头暗号)
        // 请求头 请求参数 .. 响应头 响应状态码 报文 ....
        System.out.println(responseEntity);
    }
    @Test
    void testPostJson() {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8080/testPost1";
        //封装对象
        User user = new User("changlu", 22, 1000D);
        //发送POST,而且是JSON参数,在web里面默认会使用jackson,会将对象转为字符串
        String content = restTemplate.postForObject(url, user, String.class);
        System.out.println(content);
    }
    @Test
    void testPost() {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8080/testPost2";
        //对于表单参数,使用LinkedMultiValueMap来进行封装
        LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("name", "changlu");
        map.add("age", 26);
        map.add("price", 80080);
        String content = restTemplate.postForObject(url, map, String.class);
        System.out.println(content);
    }
}
 
            【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
                cloudbbs@huaweicloud.com
                
            
        
        
        
        
        
        
        - 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)