0
0
Spring Bootframework~10 mins

@Component, @Service, @Repository, @Controller 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 declare a Spring component class.

Spring Boot
@[1]
public class MyComponent {
}
Drag options to blanks, or click blank then click option'
AController
BService
CRepository
DComponent
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service or @Repository when the class is a general component.
Forgetting the @Component annotation causes Spring not to detect the class.
2fill in blank
medium

Complete the code to declare a service class in Spring Boot.

Spring Boot
@[1]
public class UserService {
}
Drag options to blanks, or click blank then click option'
AController
BService
CRepository
DComponent
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @Service for service classes.
Confusing @Service with @Repository or @Controller.
3fill in blank
hard

Fix the error in the annotation to mark a data access class.

Spring Boot
@[1]
public class UserRepository {
}
Drag options to blanks, or click blank then click option'
AComponent
BService
CRepository
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service or @Component instead of @Repository for data access classes.
Not annotating the repository class causes Spring not to manage it.
4fill in blank
hard

Complete the code to declare a Spring MVC controller class with a request mapping.

Spring Boot
@[1]
@RequestMapping("/users")
public class UserController {
    @GetMapping
    public String listUsers() {
        return "userList";
    }
}
Drag options to blanks, or click blank then click option'
AController
BService
CComponent
DRepository
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component or @Service instead of @Controller for web controllers.
Forgetting @Controller causes Spring MVC not to detect the controller.
5fill in blank
hard

Fill all three blanks to complete a Spring Boot REST controller with service injection.

Spring Boot
@[1]
public class ProductController {

    private final ProductService productService;

    public ProductController([2] productService) {
        this.productService = productService;
    }

    @GetMapping("/products")
    public List<Product> getAllProducts() {
        return productService.[3]();
    }
}
Drag options to blanks, or click blank then click option'
ARestController
BProductService
CgetProducts
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Controller instead of @RestController for REST APIs.
Passing the wrong type in constructor parameter.
Calling a non-existent method on the service.