0
0
Spring Bootframework~10 mins

Why annotations drive Spring Boot in Spring Boot - Test Your Understanding

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

Complete the code to mark the class as a Spring Boot application.

Spring Boot
@[1]
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}
Drag options to blanks, or click blank then click option'
AComponent
BSpringBootApplication
CService
DRepository
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @SpringBootApplication
Forgetting to add any annotation on the main class
2fill in blank
medium

Complete the code to inject a service bean into the controller using annotation.

Spring Boot
@RestController
public class MyController {
    private final MyService service;

    public MyController([1] MyService service) {
        this.service = service;
    }
}
Drag options to blanks, or click blank then click option'
A@Service
B@Inject
C@Component
D@Autowired
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service instead of @Autowired on constructor parameter
Not annotating the constructor parameter at all
3fill in blank
hard

Fix the error in the code by completing the annotation to define a REST endpoint.

Spring Boot
@RestController
public class HelloController {

    @[1]("/hello")
    public String sayHello() {
        return "Hello, World!";
    }
}
Drag options to blanks, or click blank then click option'
APostMapping
BRequestMapping
CGetMapping
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service instead of a mapping annotation
Using @PostMapping for a GET endpoint
4fill in blank
hard

Fill both blanks to define a service class and mark it as a Spring bean.

Spring Boot
[1]
public class UserService {

    public String getUser() {
        return "User1";
    }
}
Drag options to blanks, or click blank then click option'
A@Service
B@Repository
C@Component
D@Controller
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Controller or @Repository incorrectly for service classes
Not annotating the class at all
5fill in blank
hard

Fill all three blanks to create a REST controller with a GET endpoint that returns a greeting.

Spring Boot
@[1]
public class GreetingController {

    @[2]("/greet")
    public String greet() {
        return [3];
    }
}
Drag options to blanks, or click blank then click option'
ARestController
BGetMapping
C"Hello from Spring Boot!"
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Controller instead of @RestController
Using @PostMapping instead of @GetMapping
Returning a variable instead of a string literal