Created
July 25, 2019 11:07
-
-
Save tyb/63ccf363354da848cf733fdcc6e3509e to your computer and use it in GitHub Desktop.
RestTemplateConfig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.xxx.common.config; | |
import com.xxx.common.util.HeaderRequestInterceptor; | |
import org.apache.cxf.common.util.CollectionUtils; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.http.client.ClientHttpRequestFactory; | |
import org.springframework.http.client.ClientHttpRequestInterceptor; | |
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | |
import org.springframework.web.client.RestTemplate; | |
import java.util.ArrayList; | |
import java.util.List; | |
@Configuration | |
public class RestTemplateConfig { | |
/* @Bean | |
public RestTemplate restTemplate(RestTemplateBuilder builder) { | |
return builder.build(); | |
}*/ | |
@Bean | |
public RestTemplate restTemplate() { | |
//return new RestTemplate(); | |
//Taha: timeout özelliği ekledim. | |
//RestTemplate restTemplate = new RestTemplate(); | |
RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory()); | |
//restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(clientHttpRequestFactory())); | |
//restTemplate.setMessageConverters(Collections.singletonList(mappingJacksonHttpMessageConverter())); | |
//restTemplate.setInterceptors( Collections.singletonList(new HeaderRequestInterceptor())); | |
List<ClientHttpRequestInterceptor> interceptors | |
= restTemplate.getInterceptors(); | |
if (CollectionUtils.isEmpty(interceptors)) { | |
interceptors = new ArrayList<>(); | |
} | |
interceptors.add(new HeaderRequestInterceptor()); | |
restTemplate.setInterceptors(interceptors); | |
return restTemplate; | |
//return restTemplate; | |
} | |
private ClientHttpRequestFactory getClientHttpRequestFactory() { | |
int timeout = 5000; | |
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory | |
= new HttpComponentsClientHttpRequestFactory(); | |
clientHttpRequestFactory.setConnectTimeout(timeout); | |
return clientHttpRequestFactory; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment