Spring RestTemplate Interceptor 配置
Spring RestTemplate Interceptor 配置
OrangeJoke 发表于1年前
Spring RestTemplate Interceptor 配置
  • 发表于 1年前
  • 阅读 143
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云实验室 1小时搭建人工智能应用,让技术更容易入门 免费体验 >>>   

摘要: spring interceptor configuration,spring restTemplate 提供统一的对外请求API,底层的实现可以通过自定义实现。本文为大家介绍interceptor的配置,用于打印日志。更多的其实是提供样板代码,直接可以在项目中使用。 spring 版本:4.2.3

自定义自己的interceptor


public class ScClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
     @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution         execution) throws IOException {
                //重写实现方法
            }
}

配置httpclient (用于发送请求),并为restTemplate注入interceptor

   @Bean
    @ConditionalOnProperty(value = "com.social.credits.infra.fiber.enabled", matchIfMissing = true)
    public HttpClient httpClient() {
        return FiberHttpClientBuilder.
                create(httpIoThread). // use io threads
                setMaxConnPerRoute(httpConnPerRoute).
                setMaxConnTotal(httpConnMax).build();
    }

    /**
     * set restTemplate http client
     * 构造使用Apache的httpclient
     * if use restTemplate.getInterceptors().add(new ScClientHttpRequestInterceptor()); may get null from result
     */
    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient());
        factory.setConnectionRequestTimeout(connectionRequestTimeout);
        factory.setReadTimeout(readTimeout);
        // 构造加入interceptor
        List<ClientHttpRequestInterceptor> interceptorList = new ArrayList<>();
        interceptorList.add(new ScClientHttpRequestInterceptor());  //自己实现的interceptor

        InterceptingClientHttpRequestFactory interceptorFactory = new InterceptingClientHttpRequestFactory(new BufferingClientHttpRequestFactory(factory), interceptorList);
        restTemplate.setRequestFactory(interceptorFactory);
        return restTemplate;
    }

总结

在为RestTemplate注入interceptor的时候要选择 InterceptingClientHttpRequestFactory interceptorFactory = new InterceptingClientHttpRequestFactory(new BufferingClientHttpRequestFactory(factory), interceptorList); 这个构造函数,不然会导致restTemplate getbody return null error

参考链接:

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/client/ClientHttpRequestInterceptor.html

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/client/BufferingClientHttpRequestFactory.html (getbody return null!!)

http://stackoverflow.com/questions/33372859/spring-resttemplate-bufferingclienthttprequestfactory-simpleclienthttpreques

http://springinpractice.com/2013/11/01/how-to-pretty-print-your-json-with-spring-and-jackson/ http://www.ibm.com/developerworks/library/j-tracemt/index.html

共有 人打赏支持
粉丝 29
博文 30
码字总数 23836
×
OrangeJoke
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: