Skip to content

Instantly share code, notes, and snippets.

@kartikshah
Created July 22, 2016 20:46
Show Gist options
  • Save kartikshah/7261a89bd42e5d839afda6329e1b6d1a to your computer and use it in GitHub Desktop.
Save kartikshah/7261a89bd42e5d839afda6329e1b6d1a to your computer and use it in GitHub Desktop.
sample AppConfig
@Configuration
@ComponentScan(basePackageClasses = Application.class)
public class AppConfig
{
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate(httpRequestFactory());
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
for (HttpMessageConverter<?> converter : converters) {
if (converter instanceof MappingJackson2HttpMessageConverter) {
MappingJackson2HttpMessageConverter jsonConverter = (MappingJackson2HttpMessageConverter) converter;
jsonConverter.setObjectMapper(new ObjectMapper());
jsonConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("application", "json", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET), new MediaType("text", "javascript", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET)));
}
}
return restTemplate;
}
@Bean
public ClientHttpRequestFactory httpRequestFactory() {
return new HttpComponentsClientHttpRequestFactory(httpClient());
}
@Bean
public HttpClient httpClient() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
connectionManager.setMaxTotal(5);
connectionManager.setDefaultMaxPerRoute(5);
return closeableHttpClient;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment