Jordi
Jordi

Reputation: 23207

Spring: Inject initialized RestTemplates

Currently I have several services making requests to other services.

In order to build the requests I'm using Spring RestTemplates.

Is there anyway to inject a RestTemplate initialized with some headers...?

I was thinking about Factory injectors.

Any ideas?

Upvotes: 1

Views: 77

Answers (1)

DwB
DwB

Reputation: 38300

I think you are approaching the problem incorrectly.

Instead of injecting a RestTemplate, consider injecting an HttpEntity<?>.

Setup the HttpEntity<?> bean in your configuration @Bean method.

Then use which ever RestTemplate.exchange() method is appropriate to send your desired request.

Edits
The exchange method sends an HTTP message and returns the response.

Here is the RestTemplate JavaDoc Page

Upvotes: 1

Related Questions