• RestTemplate 集成拦截器


    一 RestTemplate 为什么集成拦截器

    Spring RestTemplate 经常被用作客户端向 Restful API 发送各种请求,在开发过程中我们会发现很多请求都会有相同的场景,如请求携带认证的 token、验证的签名、打印请求和响应日志等。在请求方法里面添加这些通用的非业务逻辑,代码显得很冗余。

    这个时候我们就思考,我们是不是就可以将这些非业务性代码抽取出来进行复用。

    Spring 提供了 ClientHttpRequestInterceptor 接口,可以对请求进行拦截,并在其被发送至服务端之前修改请求或是增强相应的信息。
     

    二 RestTemplate 如何集成拦截器

    2.1 实现 ClientHttpRequestInterceptor 接口

    LogInterceptor 代码
    1. import lombok.extern.slf4j.Slf4j;
    2. import org.springframework.http.HttpRequest;
    3. import org.springframework.http.client.ClientHttpRequestExecution;
    4. import org.springframework.http.client.ClientHttpRequestInterceptor;
    5. import org.springframework.http.client.ClientHttpResponse;
    6. import org.springframework.stereotype.Component;
    7. import java.io.IOException;
    8. import java.util.Optional;
    9. @Slf4j
    10. @Component
    11. public class LogInterceptor implements ClientHttpRequestInterceptor {
    12. @Override
    13. public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
    14. ClientHttpResponse response = clientHttpRequestExecution.execute(httpRequest, bytes);
    15. log.info("request = {}, requestBody = {}, response = {}", httpRequest,
    16. Optional.of(bytes).map(String::new).orElse(""), response);
    17. return response;
    18. }
    19. }
    TokenInterceptor 代码
    1. import lombok.extern.slf4j.Slf4j;
    2. import org.springframework.http.HttpHeaders;
    3. import org.springframework.http.HttpRequest;
    4. import org.springframework.http.client.ClientHttpRequestExecution;
    5. import org.springframework.http.client.ClientHttpRequestInterceptor;
    6. import org.springframework.http.client.ClientHttpResponse;
    7. import org.springframework.stereotype.Component;
    8. import java.io.IOException;
    9. @Slf4j
    10. @Component
    11. public class TokenInterceptor implements ClientHttpRequestInterceptor {
    12. @Override
    13. public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
    14. HttpHeaders headers = httpRequest.getHeaders();
    15. headers.add("Authorization", "token");
    16. return clientHttpRequestExecution.execute(httpRequest, bytes);
    17. }
    18. }

     2.2 拦截器增加到 RestTemplate 实例

    1. import com.google.common.collect.Lists;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.context.annotation.Configuration;
    5. import org.springframework.http.client.ClientHttpRequestInterceptor;
    6. import org.springframework.web.client.RestTemplate;
    7. import java.util.List;
    8. @Configuration
    9. public class RestTemplateConfig {
    10. @Autowired
    11. private LogInterceptor logInterceptor;
    12. @Autowired
    13. private TokenInterceptor tokenInterceptor;
    14. @Bean
    15. public RestTemplate restTemplate() {
    16. RestTemplate restTemplate = new RestTemplate();
    17. List interceptorList = Lists.newArrayList();
    18. // 增加多个拦截器,多个拦截器按照顺序执行
    19. interceptorList.add(tokenInterceptor);
    20. interceptorList.add(logInterceptor);
    21. restTemplate.setInterceptors(interceptorList);
    22. return restTemplate;
    23. }
    24. }

     2.3 RestTemplate 的使用

    SpringBoot 集成 RestTemplate 和使用_新猿一马的博客-CSDN博客_resttemplate 依赖目录一 RestTemplate 依赖二 RestTemplate 装配三RestTemplate 使用一 RestTemplate 依赖 org.springframework.boot spring-boot-starte...https://blog.csdn.net/jack1liu/article/details/105572375

  • 相关阅读:
    网络编程基础(一):OSI 、TCP/IP协议与hello socket:用java实现第一个TCP连接
    思腾云计算
    “先进”的飞书为何搞不定钉钉?
    Qt开发流程
    大结果集SQL引发的ClickHouse空闲超时
    【力扣刷题】Leetcode 2592 最大化数组的伟大值
    Nuxt3 实战 (五):Header 头部布局
    npm、cnpm、yarn、pnpm
    什么是物联网(IoT)?
    【GO-OpenCV】go-cv快速配置
  • 原文地址:https://blog.csdn.net/jack1liu/article/details/126924647