CustomRestTemplateConfig.java 913 Bytes
package com.yohoufo.user.common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class CustomRestTemplateConfig {

    /**
     * 定义一个500毫秒超时的RestTemplate,调用银联接口用
     * @return
     */
    @Bean
    public RestTemplate authorizeBankRestTemplate(){
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectionRequestTimeout(500);
        httpRequestFactory.setConnectTimeout(500);
        httpRequestFactory.setReadTimeout(500);

        RestTemplate restTemplate= new RestTemplate(httpRequestFactory);
        return restTemplate;
    }
}