Complete the code to create a RestTemplate bean for service communication.
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class AppConfig { @Bean public RestTemplate [1]() { return new RestTemplate(); } }
The method name restTemplate is the standard bean name for RestTemplate in Spring Boot.
Complete the code to send a GET request to another service using RestTemplate.
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class UserService { @Autowired private RestTemplate restTemplate; public String getUserData() { String url = "http://userservice/api/users/1"; return restTemplate.[1](url, String.class); } }
getForObject sends a GET request and returns the response body converted to the specified class.
Fix the error in the RestTemplate call to send a POST request with a request body.
public String createUser(String userJson) {
String url = "http://userservice/api/users";
return restTemplate.[1](url, userJson, String.class);
}postForObject sends a POST request with a request body and returns the response body.
Fill both blanks to create a Feign client interface for service-to-service calls.
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.[1]; import org.springframework.web.bind.annotation.PathVariable; @FeignClient(name = "user-service") public interface UserClient { @[2]("/api/users/{id}") String getUserById(@PathVariable("id") String id); }
Use @GetMapping annotation to define a GET request in Feign client.
Fill the two blanks to configure a WebClient bean with a base URL for service communication.
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.reactive.function.client.WebClient; @Configuration public class WebClientConfig { @Bean public WebClient [1]() { return WebClient.builder() .baseUrl([2]) .build(); } }
The bean method is named webClient. The base URL is set to "http://orderservice/api".