自定义restTemplate配置

 Maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dj</groupId>
    <artifactId>resttemplateTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.28</version>
        </dependency>

        <!--web启动依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--测试包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

application.yml 

spring:
  application:
    name: ababa
server:
  port: 8888

resttemplate:
  parameter:
    connectTimeout: 6000
    readTimeout: 6000

 配置类

package com.dj.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@RefreshScope //动态加载配置文件(可不加)
@Configuration
public class RestTemplateConfig {

    //连接超时时间,当配置文件没设定时间时,默认6000ms
    @Value("${resttemplate.parameter.connectTimeout:6000}") 
    private Integer connectTimeout;  

    //读取超时时间,当配置文件没设定时间时,默认6000ms
    @Value("${resttemplate.parameter.readTimeout:6000}")
    private Integer readTimeout;    
  
    @Bean
    @LoadBalanced //客户端对服务器的负载均衡
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        return new RestTemplate(factory);
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(readTimeout);
        factory.setConnectTimeout(connectTimeout);
        return factory;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用RestTemplate时,你可以通过配置类来自定义配置RestTemplate的一些属性和行为。下面是一个示例的RestTemplate配置类: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.BufferingClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate() { RestTemplate restTemplate = new RestTemplate(); // 配置请求工厂,可以设置连接超时时间、读取超时时间等 SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(5000); // 连接超时时间为5秒 requestFactory.setReadTimeout(5000); // 读取超时时间为5秒 restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(requestFactory)); // 添加自定义的拦截器,可以在请求和响应阶段添加额外的逻辑 restTemplate.setInterceptors(new CustomInterceptor()); // 可以设置消息转换器,用于处理请求和响应的数据转换 restTemplate.setMessageConverters(Collections.singletonList(new MappingJackson2HttpMessageConverter())); return restTemplate; } } ``` 在上面的配置类中,我们通过`@Configuration`注解将这个类标记为一个配置类,并使用`@Bean`注解创建了一个名为"restTemplate"的RestTemplate对象。 在`restTemplate()`方法中,我们可以根据需要进行一些自定义配置,例如设置连接超时时间、读取超时时间,添加拦截器以及配置消息转换器等。这里只是给出了一些常见的配置示例,你可以根据自己的需求进行进一步的配置。 使用这个配置类后,你可以在其他组件或服务中直接通过依赖注入来使用这个RestTemplate对象,而不需要每次都手动创建和配置RestTemplate。 注意:在Spring Boot 2.1版本及以上,官方推荐使用`WebClient`来替代`RestTemplate`,因为`WebClient`更加灵活和功能强大。但如果你仍然选择使用`RestTemplate`,以上的配置类可以满足大部分的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值