Last active
March 14, 2019 14:33
-
-
Save wpik/e45cb5d644bc54de0d2336675db926a2 to your computer and use it in GitHub Desktop.
RestTemplate with overriden dns to localhost
This file contains hidden or 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 example; | |
| import org.apache.http.HttpRequest | |
| import org.apache.http.HttpResponse | |
| import org.apache.http.ProtocolException | |
| import org.apache.http.client.RedirectStrategy | |
| import org.apache.http.client.methods.HttpUriRequest | |
| import org.apache.http.conn.DnsResolver | |
| import org.apache.http.impl.client.HttpClientBuilder | |
| import org.apache.http.protocol.HttpContext | |
| 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 | |
| class RestTemplateWithDnsOverrideConfig { | |
| @Bean | |
| RestTemplate createRestTemplate() { | |
| return new RestTemplate( | |
| new HttpComponentsClientHttpRequestFactory( | |
| HttpClientBuilder.create() | |
| .setDnsResolver(createDnsResolver()) | |
| .build() | |
| ) | |
| ) | |
| } | |
| private DnsResolver createDnsResolver() { | |
| return new DnsResolver() { | |
| @Override | |
| InetAddress[] resolve(String host) throws UnknownHostException { | |
| return [Inet4Address.getByAddress([127, 0, 0, 1] as byte[])] | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or 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 example; | |
| import org.apache.http.HttpRequest | |
| import org.apache.http.HttpResponse | |
| import org.apache.http.ProtocolException | |
| import org.apache.http.client.RedirectStrategy | |
| import org.apache.http.client.methods.HttpUriRequest | |
| import org.apache.http.conn.DnsResolver | |
| import org.apache.http.impl.client.HttpClientBuilder | |
| import org.apache.http.protocol.HttpContext | |
| 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 | |
| class RestTemplateWithDnsOverrideConfig { | |
| @Bean | |
| RestTemplate createRestTemplate() { | |
| return new RestTemplate( | |
| new HttpComponentsClientHttpRequestFactory( | |
| HttpClientBuilder.create() | |
| .setRedirectStrategy(createRedirectStrategy()) | |
| .build() | |
| ) | |
| ) | |
| } | |
| private RedirectStrategy createRedirectStrategy() { | |
| return new RedirectStrategy() { | |
| @Override | |
| boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { | |
| return false | |
| } | |
| @Override | |
| HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { | |
| return null | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment