Hi to everyone.
I use RestTemplate (and apach HttpClient 4) in my project and I enjoy things this template provide.
Our system has a lot of interfaces and some of services I request require to use the same local ip adrress for each request, so I store it and set property org.apache.http.conn.params.ConnRoutePNames.LOCAL_ ADDRESS every time when I execute my requests. And there's no problem if I do without RestTemplate, but I don't know how to set this parameter to request thought RestTemplate.
Actually I have some idea about it. I read RestTemplate docs and I found that I have to use RequestCallback to prepare my request before execution (requestCallback object that prepares the request).
I think about some RequestCallback implementation and it's using like that:
Code:restTemplate.execute("http://someurl", HttpMethod.GET, new LocalIpRequestCallback("127.0.0.1"), null); private final class LocalIpRequestCallback implements RequestCallback{ private final String localIp2use; private LocalIpRequestCallback(String localIp2use) { this.localIp2use = localIp2use; } @Override public void doWithRequest(ClientHttpRequest request) throws IOException { if(localIp2use != null){ if(request instanceof HttpComponentsClientHttpRequest){ HttpComponentsClientHttpRequest apacheRequest = (HttpComponentsClientHttpRequest)request; apacheRequest.setParameter(ConnRoutePNames.LOCAL_ADDRESS, localIp2use); } } } }
But there's some problems:
- HttpComponentsClientHttpRequest is not public;
- HttpComponentsClientHttpRequest even if it was a public it doesn't have any method for setting parameters;
Does anybody has any advices ?


Reply With Quote
