当前位置: 首页 > article >正文

springboot配置并使用RestTemplate

目录

一、RestTemplate配置

1、将RestTemplate初始化为Bean

2、使用HttpClient作为RestTemplate客户端

(1)引入HttpClient依赖

(2)修改RestTemplate配置类

3、设置拦截器

(1)新增拦截器类

(2)设置拦截器

4、新增支持的媒体类型

二、RestTemplate使用

1、RestTemplate注入

2、无参数get请求测试

(1)Controller代码

(2)RestTemplate单元测试

3、带参get请求测试

(1)Controller代码

(2)RestTemplate单元测试

4、带占位符参数的get请求测试

(1)Controller代码

(2)RestTemplate单元测试

5、post请求测试

(1)Article实体类

(2)Controller代码

(3)RestTemplate单元测试

6、设置请求头

(1)Article实体类

(2)Controller代码

(3)RestTemplate单元测试

7、上传文件

(1)Controller代码

(2)RestTemplate单元测试

8、文件下载

(1)Controller代码

(2)RestTemplate单元测试

三、参考


一、RestTemplate配置

1、将RestTemplate初始化为Bean

package com.xiaobai.conf;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

/**
 * @Author 王天文
 * @Date 2024/12/29 18:06
 * @Description: RestTemplate配置类
 */
@Configuration
public class RestTemplateConf {

    /**
     * 当指定类型Bean不存在时,会创建一个新的Bean,如果用户自定义了Bean,将使用用户自定义的Bean
     * @return
     */
    @ConditionalOnMissingBean(RestTemplate.class)
    @Bean
    public RestTemplate restTemplate() {
        // 使用JDK自带的HttpURLConnection作为客户端
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate;
    }
}

2、使用HttpClient作为RestTemplate客户端

(1)引入HttpClient依赖

        <!--httpclient-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.7</version>
        </dependency>

(2)修改RestTemplate配置类

package com.xiaobai.conf;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

/**
 * @Author 王天文
 * @Date 2024/12/29 18:06
 * @Description: RestTemplate配置类
 */
@Configuration
public class RestTemplateConf {

    /**
     * 当指定类型Bean不存在时,会创建一个新的Bean,如果用户自定义了Bean,将使用用户自定义的Bean
     * @return
     */
    @ConditionalOnMissingBean(RestTemplate.class)
    @Bean
    public RestTemplate restTemplate() {
        // 使用httpclient作为底层客户端
        RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());
        return restTemplate;
    }

    /**
     * 使用httpclient作为底层客户端
     * @return
     */
    @Bean
    public ClientHttpRequestFactory getClientHttpRequestFactory() {
        int timeout = 50000;

        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(timeout)
                .setConnectionRequestTimeout(timeout)
                .setSocketTimeout(timeout)
                .build();

        CloseableHttpClient httpClient = HttpClientBuilder.create()
                .setDefaultRequestConfig(requestConfig)
                .build();
        return new HttpComponentsClientHttpRequestFactory(httpClient);
    }
}

3、设置拦截器

(1)新增拦截器类

package com.xiaobai.conf;

import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

/**
 * @Author 王天文
 * @Date 2024/12/22 21:51
 * @Description: restTemplate拦截器
 */
public class RestTemplateInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest httpRequest,
                                        byte[] bytes,
                                        ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {

        ClientHttpResponse httpResponse = clientHttpRequestExecution.execute(httpRequest, bytes);
        return httpResponse;
    }
}

(2)设置拦截器

package com.xiaobai.conf;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

/**
 * @Author 王天文
 * @Date 2024/12/29 18:06
 * @Description: RestTemplate配置类
 */
@Configuration
public class RestTemplateConf {

    /**
     * 当指定类型Bean不存在时,会创建一个新的Bean,如果用户自定义了Bean,将使用用户自定义的Bean
     * @return
     */
    @ConditionalOnMissingBean(RestTemplate.class)
    @Bean
    public RestTemplate restTemplate() {
        // 使用JDK自带的HttpURLConnection作为客户端
        RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());

        // 设置拦截器
        restTemplate.setInterceptors(Collections.singletonList(restTemplateInterceptor()));
        return restTemplate;
    }

    /**
     * 使用httpclient作为底层客户端
     * @return
     */
    @Bean
    public ClientHttpRequestFactory getClientHttpRequestFactory() {
        int timeout = 50000;

        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(timeout)
                .setConnectionRequestTimeout(timeout)
                .setSocketTimeout(timeout)
                .build();

        CloseableHttpClient httpClient = HttpClientBuilder.create()
                .setDefaultRequestConfig(requestConfig)
                .build();
        return new HttpComponentsClientHttpRequestFactory(httpClient);
    }

    /**
     * 拦截器
     * @return
     */
    @Bean
    public RestTemplateInterceptor restTemplateInterceptor() {
        return new RestTemplateInterceptor();
    }
}

4、新增支持的媒体类型

RestTemplate 只支持application/json格式,需要手动补充text/plan,text/html格式

package com.xiaobai.conf;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.Collections;

/**
 * @Author 王天文
 * @Date 2024/12/29 18:06
 * @Description: RestTemplate配置类
 */
@Configuration
public class RestTemplateConf {

    /**
     * 当指定类型Bean不存在时,会创建一个新的Bean,如果用户自定义了Bean,将使用用户自定义的Bean
     * @return
     */
    @ConditionalOnMissingBean(RestTemplate.class)
    @Bean
    public RestTemplate restTemplate() {
        // 使用JDK自带的HttpURLConnection作为客户端
        RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());

        // 设置拦截器
        restTemplate.setInterceptors(Collections.singletonList(restTemplateInterceptor()));

        // 增加支持的媒体类型
        restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter());
        return restTemplate;
    }

    /**
     * 使用httpclient作为底层客户端
     * @return
     */
    @Bean
    public ClientHttpRequestFactory getClientHttpRequestFactory() {
        int timeout = 50000;

        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(timeout)
                .setConnectionRequestTimeout(timeout)
                .setSocketTimeout(timeout)
                .build();

        CloseableHttpClient httpClient = HttpClientBuilder.create()
                .setDefaultRequestConfig(requestConfig)
                .build();
        return new HttpComponentsClientHttpRequestFactory(httpClient);
    }

    /**
     * 拦截器
     * @return
     */
    @Bean
    public RestTemplateInterceptor restTemplateInterceptor() {
        return new RestTemplateInterceptor();
    }

    /**
     * 媒体类型
     * @return
     */
    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        // RestTemplate 只支持application/json格式,需要手动补充text/html格式
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN, MediaType.TEXT_HTML));

        return mappingJackson2HttpMessageConverter;
    }
}

二、RestTemplate使用

1、RestTemplate注入

    @Autowired
    private RestTemplate restTemplate;

2、无参数get请求测试

(1)Controller代码

    @GetMapping(value = "/getString")
    public String getString() {
        return "操作成功";
    }

(2)RestTemplate单元测试

    @Test
    public void testGetString() {
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://localhost:8090/getString", String.class);
        log.info(responseEntity.getBody());
    }

3、带参get请求测试

(1)Controller代码

    @GetMapping("/getRequestByParam")
    public Map<String, Object> getRequestByParam(@RequestParam("name") String name) {
        log.info("名称:" + name);

        Map<String, Object> map = new HashMap<>();
        map.put("responseData", "请求成功");
        return map;
    }

(2)RestTemplate单元测试

    @Test
    public void testParamGet() throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("name", "张三");
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://localhost:8090/getRequestByParam?name={name}", String.class, param);

        log.info("响应信息:{}", responseEntity.getBody());
    }

4、带占位符参数的get请求测试

(1)Controller代码

    @GetMapping("/getRequestByPlaceHolder/{name}/{age}")
    public Map<String, Object> getRequestByPlaceHolder(@PathVariable("name") String name,
                                                       @PathVariable("age") String age) {
        log.info("名称:" + name);
        log.info("年龄:" + age);

        Map<String, Object> map = new HashMap<>();
        map.put("responseData", "请求成功");
        return map;
    }

(2)RestTemplate单元测试

    @Test
    public void testPlaceholderGet() {
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://localhost:8090/getRequestByPlaceHolder/{1}/{2}", String.class, "张三", "25");

        log.info("响应信息:{}", responseEntity.getBody());
    }

5、post请求测试

(1)Article实体类

package com.xiaobai.aroundtest.entity;

import lombok.Data;

import java.io.Serializable;

/**
 * @author wangtw
 * @date 2023/12/6 0:35
 * @description
 */
@Data
public class Article implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 文章名称
     */
    private String name;

    /**
     * 描述
     */
    private String description;
}

(2)Controller代码

    @PostMapping("/postRequest")
    public Map<String, Object> postRequest(@RequestParam String name, @RequestBody Article article) {
        log.info("名称:" + name);

        log.info("文章名称:" + article.getName());
        log.info("文章描述:" + article.getDescription());

        Map<String, Object> map = new HashMap<>();
        map.put("responseData", "请求成功");
        return map;
    }

(3)RestTemplate单元测试

    @Test
    public void testPost() {

        // 表单数据
        Map<String, Object> formData = new HashMap<>();
        formData.put("name", "解忧杂货店");
        formData.put("description", "这是一本好书");

        // 单独传参
        Map<String, Object> param = new HashMap<>();
        param.put("name", "东野圭吾");

        // 请求调用
        HttpEntity<Map<String, Object>> formEntity = new HttpEntity<>(formData);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://localhost:8090/postRequest?name={name}", formEntity, String.class, param);
        log.info("响应信息:{}", responseEntity.getBody());
    }

6、设置请求头

(1)Article实体类

package com.xiaobai.aroundtest.entity;

import lombok.Data;

import java.io.Serializable;

/**
 * @author wangtw
 * @date 2023/12/6 0:35
 * @description
 */
@Data
public class Article implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 文章名称
     */
    private String name;

    /**
     * 描述
     */
    private String description;
}

(2)Controller代码

    @PostMapping("/postRequestHeader")
    public Map<String, Object> postRequestHeader(HttpServletRequest request,
                                                 @RequestParam String name, @RequestBody Article article) {

        String token = request.getHeader("token");
        log.info("请求token:" + token);

        log.info("名称:" + name);

        log.info("文章名称:" + article.getName());
        log.info("文章描述:" + article.getDescription());

        Map<String, Object> map = new HashMap<>();
        map.put("responseData", "请求成功");
        return map;
    }

(3)RestTemplate单元测试

    @Test
    public void testPostHeader() {

        // 请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("token", "123456");

        // 表单数据
        Map<String, Object> formData = new HashMap<>();
        formData.put("name", "解忧杂货店");
        formData.put("description", "这是一本好书");

        // 单独传参
        Map<String, Object> param = new HashMap<>();
        param.put("name", "东野圭吾");

        // 请求调用
        HttpEntity<Map<String, Object>> formEntity = new HttpEntity<>(formData, httpHeaders);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://localhost:8090/postRequestHeader?name={name}", formEntity, String.class, param);
        log.info("响应信息:{}", responseEntity.getBody());
    }

7、上传文件

(1)Controller代码

    @PostMapping("/upload")
    public Map<String, Object> upload(@RequestParam String name, MultipartFile uploadFile) throws IOException {
        log.info("名称:" + name);

        uploadFile.transferTo(new File("D:\\temp/" + uploadFile.getOriginalFilename()));

        Map<String, Object> map = new HashMap<>();
        map.put("responseData", "请求成功");
        return map;
    }

(2)RestTemplate单元测试

    @Test
    public void testUploadFile() {

        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("uploadFile", new FileSystemResource(new File("D:\\christmas-tree.svg")));
        param.add("name", "张三");

        // 请求头设置
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        // 请求调用
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(param, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://localhost:8090/upload", formEntity, String.class);
        log.info("响应信息:{}", responseEntity.getBody());

    }

8、文件下载

(1)Controller代码

    @PostMapping("/download")
    public Map<String, Object> download(@RequestParam String fileName,
                                        HttpServletResponse response) {
        log.info("文件名称:" + fileName);

        File file = new File("D:\\temp/" + fileName);

        try(FileInputStream fileInputStream = new FileInputStream(file);
            ServletOutputStream outputStream = response.getOutputStream()) {

            response.setHeader("content-disposition","attachment;fileName=" + URLEncoder.encode(fileName,"UTF-8"));

            FileCopyUtils.copy(fileInputStream, outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Map<String, Object> map = new HashMap<>();
        map.put("responseData", "请求成功");
        return map;
    }

(2)RestTemplate单元测试

    @Test
    public void testDownloadFile() {
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("fileName", "christmas-tree.svg");

        // 请求调用
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(param);
        ResponseEntity<byte[]> responseEntity = restTemplate.postForEntity("http://localhost:8090/download", formEntity, byte[].class);

        // 获取响应头
        HttpHeaders responseEntityHeaders = responseEntity.getHeaders();
        Set<Map.Entry<String, List<String>>> responseSet = responseEntityHeaders.entrySet();
        for (Map.Entry<String, List<String>> responseValue : responseSet) {
            log.info("响应头:" + responseValue.getKey() + ",响应内容:" + responseValue.getValue());
        }

        try {
            // 文件保存
            byte[] fileData = responseEntity.getBody();
            FileCopyUtils.copy(fileData, new File("D:\\christmas-tree1.svg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

三、参考

Spring之RestTemplate详解


http://www.kler.cn/a/463936.html

相关文章:

  • 高频java面试题
  • P5289 [十二省联考 2019] 皮配 做题记录
  • 框架(Mybatis基础配置)
  • 【现代摄像头作为一种视频输入摄像头】
  • FFmpeg 4.3 音视频-多路H265监控录放C++开发二十一.4,SDP协议分析
  • vscode 多项目冲突:进行 vscode 工作区配置
  • 攻防世界web第十题Web_python_template_injection
  • DDSort-简单实用的jQuery拖拽排序插件
  • NLP论文速读(NeurIPS 2024)|BERT作为生成式上下文学习者BERTs are Generative In-Context Learners
  • Microsoft SQL Server Integration Services (SSIS) 详细介绍
  • 树型DP # 战略游戏
  • 【JS】期约的Promise.all()和 Promise.race()区别
  • MySQL SQL元查询详解(10k,含运行实例、分析)
  • 验证二叉搜索树
  • LeetCode-最长公共前缀(014)
  • 闯关leetcode——3136. Valid Word
  • C++软件设计模式之责任链模式
  • 【2024年-12月-18日-开源社区openEuler实践记录】openeuler - jenkins:开源项目持续集成与交付的幕后引擎
  • OpenCV调整图像亮度和对比度
  • 【NLP高频面题 - LLM训练篇】为什么要对LLM做有监督微调(SFT)?
  • 使用apisix+oidc+casdoor配置微服务网关
  • 第二讲 比特币的技术基础
  • GPU 进阶笔记(三):华为 NPU/GPU 演进
  • 【Spring MVC 异常处理机制】应对意外情况
  • Pandas-数据分组
  • Seata AT 模式两阶段过程原理解析【seata AT模式如何做到对业务的无侵入】