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常用方法

  • restTemplate是spring封装的http请求调用模板

get请求

  • 普通get请求
        RestTemplate restTemplate = new RestTemplate();
        // url请求的地址,第二个参数接收http返回结果的类型
        String url = restTemplate.getForObject("url", String.class);
  • 使用UriComponentsBuilder进行参数拼接
        RestTemplate restTemplate = new RestTemplate();
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("url")
                .queryParam("page", 1)
                .queryParam("limit", 10)
                .queryParam("param1", "value1");

        String resp = restTemplate.getForObject(builder.build().toString(), String.class);
  • get请求携带header信息
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        // 请求参数和header
        HttpEntity entity = new HttpEntity<>(headers);
        ResponseEntity<String> responseEntity = restTemplate.exchange("url", HttpMethod.GET, entity, String.class);

post请求

  • 提交表单 APPLICATION_FORM_URLENCODED
        RestTemplate restTemplate = new RestTemplate();
        // 设置头信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        // 设置请求boby
        MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
        body.add("param1", "value1");
        body.add("param2", "value2");

        HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(body, headers);
        String resp = restTemplate.postForObject("url", entity, String.class);
  • 发送JSON格式请求
        RestTemplate restTemplate = new RestTemplate();
        // 设置头信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 设置请求boby body也可以换成java对象
        Map<String, String> body = new HashMap<>();
        body.put("param1", "value1");
        body.put("param2", "value2");

        HttpEntity<Map<String, String>> entity = new HttpEntity<>(body, headers);
        String resp = restTemplate.postForObject("url", entity, String.class);
  • 发送body加密之后的数据
        RestTemplate restTemplate = new RestTemplate();
        // 设置头信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 设置请求boby body也可以换成java对象
        Map<String, String> body = new HashMap<>();
        body.put("param1", "value1");
        body.put("param2", "value2");
        // 加密body
        String encryptBody="把body进行加密";
        HttpEntity<String> entity = new HttpEntity<>(encryptBody, headers);

        // 添加自定义消息转换器
        restTemplate.getMessageConverters().add(new CustomJacksonHttpMessageConverter());
        String resp = restTemplate.postForObject("url", entity, String.class);

自定义消息转换器

  • restTemplate默认的是不是解释html返回类型的数据的
  • 自定义信息转换器
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.util.Arrays;

/**
 * @author liouwb
 */
public class CustomJacksonHttpMessageConverter extends MappingJackson2HttpMessageConverter {
    public CustomJacksonHttpMessageConverter() {
        setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN, 
                MediaType.APPLICATION_JSON, MediaType.TEXT_HTML, 
                new MediaType("application", "*+json")));
    }
}
  • 在使用的时候添加自定义信息转换器
        RestTemplate restTemplate = new RestTemplate();
        // 设置头信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 设置请求boby body也可以换成java对象
        Map<String, String> body = new HashMap<>();
        body.put("param1", "value1");
        body.put("param2", "value2");
        HttpEntity<Map<String, String>> entity = new HttpEntity<>(body, headers);

        // 添加自定义消息转换器
        restTemplate.getMessageConverters().add(new CustomJacksonHttpMessageConverter());
        String resp = restTemplate.postForObject("url", entity, String.class);

springboot项目中使用restTemplate并添加自定义消息转换器

/**
 * @author liouwb
 */
@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        RestTemplate build = builder.build();
        List<HttpMessageConverter<?>> messageConverters = build.getMessageConverters();
        messageConverters.add(new CustomJacksonHttpMessageConverter());
        return build;
    }
}
  网络协议 最新文章
使用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:15 
 
开发: 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:19:32-

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