I am trying to follow the best practise of autowiring Webclient using WebClient Builder but little confused.
Here is my Main Application in which i am producing a Webclient Builder and autowiring it in one of my service class
@SpringBootApplication public class MyApplication { @Bean public WebClient.Builder getWebClientBuilder() { return WebClient.builder(); } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); }} ServiceImpl Class public class MyServiceImpl implements MyService { private static final String API_MIME_TYPE = "application/json"; private static final String API_BASE_URL = "http://localhost:8080"; private static final String USER_AGENT = "Spring 5 WebClient"; private static final Logger logger = LoggerFactory.getLogger(MyServiceImpl.class); @Autowired private WebClient.Builder webClientBuilder; @Override public Mono<Issue> createIssue(Fields field) { return webClientBuilder.build() .post() .uri("/rest/api/") .body(Mono.just(field), Fields.class) .retrieve() .bodyToMono(Issue.class); }} I am trying to build the webClientBuilder with BaseURl, DefaultHeader etc. I tried to initialize it inside MyServiceImpl Constructer but not sure if its correct or not.
public MyServiceImpl() { this.webClientBuilder .baseUrl(API_BASE_URL).defaultHeader(HttpHeaders.CONTENT_TYPE, API_MIME_TYPE) .defaultHeader(HttpHeaders.USER_AGENT, USER_AGENT) .build(); } Am i doing it correct or is there a better way to do it.
Currently I have 2 ServiceImpls to call Different Apis and thats the reason i tried to set the 'baseurl' and other defaults in service itself.
Please Help. TIA