0
0
Spring Bootframework~10 mins

Service-to-service communication in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a RestTemplate bean for service communication.

Spring Boot
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();
    }
}
Drag options to blanks, or click blank then click option'
ARestTemplate
BrestTemplate
CcreateRestTemplate
DgetRestTemplate
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase method name which is invalid in Java method naming conventions.
Using a name that does not match the bean usage elsewhere.
2fill in blank
medium

Complete the code to send a GET request to another service using RestTemplate.

Spring Boot
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);
    }
}
Drag options to blanks, or click blank then click option'
Aexchange
BpostForObject
Cput
DgetForObject
Attempts:
3 left
💡 Hint
Common Mistakes
Using postForObject which sends a POST request instead of GET.
Using put which is for updating resources.
3fill in blank
hard

Fix the error in the RestTemplate call to send a POST request with a request body.

Spring Boot
public String createUser(String userJson) {
    String url = "http://userservice/api/users";
    return restTemplate.[1](url, userJson, String.class);
}
Drag options to blanks, or click blank then click option'
ApostForObject
BgetForObject
CgetForEntity
Dput
Attempts:
3 left
💡 Hint
Common Mistakes
Using getForObject which does not send a request body.
Using put which does not return a response body.
4fill in blank
hard

Fill both blanks to create a Feign client interface for service-to-service calls.

Spring Boot
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);
}
Drag options to blanks, or click blank then click option'
AGetMapping
BPostMapping
CRequestMapping
DDeleteMapping
Attempts:
3 left
💡 Hint
Common Mistakes
Using PostMapping for a GET request.
Using DeleteMapping which is for deleting resources.
5fill in blank
hard

Fill the two blanks to configure a WebClient bean with a base URL for service communication.

Spring Boot
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();
    }
}
Drag options to blanks, or click blank then click option'
AwebClient
B"http://orderservice/api"
C"http://inventoryservice/api"
D"http://paymentservice/api"
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method name casing.
Forgetting quotes around the base URL string.