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

java 复制代码
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依赖

XML 复制代码
        <!--httpclient-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.7</version>
        </dependency>

(2)修改RestTemplate配置类

java 复制代码
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)新增拦截器类

java 复制代码
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)设置拦截器

java 复制代码
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格式

java 复制代码
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注入

java 复制代码
    @Autowired
    private RestTemplate restTemplate;

2、无参数get请求测试

(1)Controller代码

java 复制代码
    @GetMapping(value = "/getString")
    public String getString() {
        return "操作成功";
    }

(2)RestTemplate单元测试

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

3、带参get请求测试

(1)Controller代码

java 复制代码
    @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单元测试

java 复制代码
    @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代码

java 复制代码
    @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单元测试

java 复制代码
    @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实体类

java 复制代码
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代码

java 复制代码
    @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单元测试

java 复制代码
    @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实体类

java 复制代码
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代码

java 复制代码
    @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单元测试

java 复制代码
    @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代码

java 复制代码
    @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单元测试

java 复制代码
    @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代码

java 复制代码
    @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单元测试

java 复制代码
    @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详解

相关推荐
ggdpzhk2 小时前
idea 编辑竖列:alt +shift+insert
java·ide·intellij-idea
hikktn3 小时前
Java 兼容读取WPS和Office图片,结合EasyExcel读取单元格信息
java·开发语言·wps
迪迦不喝可乐3 小时前
软考 高级 架构师 第十一章 面向对象分析 设计模式
java·设计模式
檀越剑指大厂3 小时前
【Java基础】使用Apache POI和Spring Boot实现Excel文件上传和解析功能
java·spring boot·apache
苹果酱05673 小时前
Golang的网络流量分配策略
java·spring boot·毕业设计·layui·课程设计
小青柑-3 小时前
Go语言中的接收器(Receiver)详解
开发语言·后端·golang
孑么4 小时前
GDPU Android移动应用 重点习题集
android·xml·java·okhttp·kotlin·android studio·webview
未命名冀5 小时前
微服务面试相关
java·微服务·面试
Heavydrink5 小时前
ajax与json
java·ajax·json
阿智智5 小时前
纯手工(不基于maven的pom.xml、Web容器)连接MySQL数据库的详细过程(Java Web学习笔记)
java·mysql数据库·纯手工连接