2

My RestClient is suppose to consume services from multiple servers, each with different hostname and credentials.

I understand that RestTemplate is thread safe, and creates a different connection for every Task, but how it can be done with different credentials ? Aren't the credentials provided to the RestTemplate upon creation ?

Community
  • 1
  • 1
Nati
  • 1,034
  • 5
  • 19
  • 46
  • are they logically the same services? just with different hosts? or different services? – leeor Jan 15 '16 at 00:50
  • 1
    Both. Many services, can be divided to groups of the same service with different hosts. – Nati Jan 15 '16 at 01:02
  • 1
    Why should one do this. It is hard to configure, hard to understand and one has to write some new classes, ..., why do you not use not one RestTemplate per server? – Ralph Jan 15 '16 at 07:56
  • @Ralph, You're right. I think I'll go for a `RestTemplateFactory` and keep a `RestTemplate` for each server. – Nati Jan 15 '16 at 08:16

1 Answers1

3

You mention credentials, so I'll assume that you use the RestTemplate to call services that require authentication. Authentication is usually handled through HTTP request headers (e.g. basic HTTP auth) or URL parameters.

2 possibilities:

  • you instantiate a plain RestTemplate and handle the authentication manually (by adding the headers or URL parameters when you do a GET or POST...) => there should be no problem reusing that RestTemplate with multiple service.
  • you instantiate the RestTemplate with a custom ClientHttpRequestFactory or some custom ClientHttpRequestInterceptor that handle the security => you probably cannot share them because you might mix the different authentications.
Christophe L
  • 13,725
  • 6
  • 33
  • 33
  • 3
    Thanks. I saw that `RestTemplate` for some reason is considered a "heavy" object, so I wanted to create as little as possible, but messing around with manual authentication on each `GET` / `POST` doesn't sound much fun. I Think I'll keep a `RestTemplate` for each server I use. – Nati Jan 15 '16 at 08:18