0
0
Spring Bootframework~20 mins

Service-to-service communication in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Service-to-service communication Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this RestTemplate call?
Consider a Spring Boot service using RestTemplate to call another service endpoint that returns a JSON object {"status":"ok"}. What will be the output of the following code snippet?
Spring Boot
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject("http://localhost:8081/api/status", String.class);
System.out.println(response);
A"{\"status\":\"ok\"}"
BRestClientException
Cnull
Dstatus: ok
Attempts:
2 left
💡 Hint
The RestTemplate returns the raw response body as a String when using getForObject with String.class.
📝 Syntax
intermediate
2:00remaining
Which option correctly declares a Feign client interface?
You want to create a Feign client in Spring Boot to call a remote service at 'http://inventory-service'. Which of the following interface declarations is correct?
A
@FeignClient(url = "http://inventory-service")
public interface InventoryClient {
  @PostMapping("/items")
  List<String> getItems();
}
B
@FeignClient(name = "inventory-service")
public class InventoryClient {
  @GetMapping("/items")
  List<String> getItems();
}
C
@FeignClient(name = "inventory-service", url = "http://inventory-service")
public interface InventoryClient {
  @GetMapping("/items")
  List<String> getItems();
}
D
@FeignClient(name = "inventory-service")
public interface InventoryClient {
  @GetMapping("items")
  List<String> getItems();
}
Attempts:
2 left
💡 Hint
Feign clients must be interfaces and use correct annotations for HTTP methods and paths.
🔧 Debug
advanced
2:00remaining
Why does this WebClient call fail with a 404 error?
Given this WebClient call to a service endpoint '/api/data', the call returns 404 Not Found. What is the most likely cause?
Spring Boot
WebClient client = WebClient.create("http://localhost:8080");
String result = client.get()
  .uri("api/data")
  .retrieve()
  .bodyToMono(String.class)
  .block();
AThe URI path is missing a leading slash, so the request goes to 'http://localhost:8080api/data' which is invalid.
BThe WebClient is not configured with a base URL, so it cannot resolve the URI.
CThe retrieve() method does not handle 404 errors and throws an exception instead.
DThe block() method is called on a Mono which is not allowed in WebClient.
Attempts:
2 left
💡 Hint
Check how the URI is constructed when using WebClient with a base URL.
state_output
advanced
2:00remaining
What is the value of 'response' after this asynchronous WebClient call?
Consider this Spring Boot WebClient code snippet. What will be the value of the 'response' variable after execution?
Spring Boot
WebClient client = WebClient.create("http://localhost:8080");
final String[] response = {null};
client.get()
  .uri("/api/message")
  .retrieve()
  .bodyToMono(String.class)
  .subscribe(body -> {
    response[0] = body;
  });
System.out.println(response[0]);
AA compilation error because response is assigned inside lambda
BThe response body string from the service
CAn empty string ""
Dnull
Attempts:
2 left
💡 Hint
Remember that subscribe runs asynchronously and the print happens immediately.
🧠 Conceptual
expert
2:00remaining
Which statement best describes the difference between RestTemplate and WebClient in Spring Boot?
Choose the most accurate description of how RestTemplate and WebClient differ in service-to-service communication.
AWebClient is only used for SOAP services, while RestTemplate is for REST services.
BRestTemplate is synchronous and blocking, while WebClient supports asynchronous and non-blocking calls.
CBoth RestTemplate and WebClient are deprecated and should not be used for service communication.
DRestTemplate supports reactive streams natively, but WebClient only supports traditional blocking calls.
Attempts:
2 left
💡 Hint
Think about how each client handles request execution and threading.