I have one question regarding Spring Framework WebClient
In my application I need to do many similar API calls, sometimes I need to change headers in the calls (Authentication token). So the question arises, what would be better of the two options:
- To create one WebClient for all incoming requests to MyService.class, by making it
private final
field, like code below:
private final WebClient webClient = WebClient.builder()
.baseUrl("https://another_host.com/api/get_inf")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.build();
Here arises another question: is WebClient
thread-safe? (because service is used by many threads)
- To create new WebClient for each new request incoming to service class.
I want to provide maximum performance and to use it in the right way, but I don't know how WebClient
works inside it, and how it expects to be used.
Thank you.