@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Service
public class ExampleService {
private final RestTemplate restTemplate;
public String getData() {
return restTemplate.getForObject(
"http://example.com/api",
String.class
);
}
}
@Configuration
public class WebClientConfig {
@Bean
public WebClient webClient() {
return WebClient.builder()
.baseUrl("http://api.example.com")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.build();
}
}
@Service
public class ExampleService {
private final WebClient webClient;
public Mono<UserResponse> getUser(String userId) {
return webClient.get()
.uri("/users/{id}", userId)
.retrieve()
.bodyToMono(UserResponse.class);
}
}