If you need to create a Spring Boot component responsible for handling HTTP requests and responses, which annotation should you use, and how would you set up the methods within this component?
- @Controller with methods annotated as @ResponseBody.
- @Repository with methods annotated as @PostMapping.
- @RestController with methods annotated as @RequestMapping.
- @Service with methods annotated as @GetMapping.
In Spring Boot, you would use the @RestController annotation for creating components that handle HTTP requests and responses. Methods within this component should be annotated with @RequestMapping or its shortcut annotations like @GetMapping, @PostMapping, etc., to define the request mapping for each method. The @RestController annotation combines @Controller and @ResponseBody, making it suitable for RESTful web services.
Loading...
Related Quiz
- Which Java utility is primarily used for monitoring Java applications and troubleshoot performance issues?
- In which scenario would you use the @ConditionalOnProperty annotation in Auto Configuration?
- You need to inject a collection of beans in a certain order in your Spring Boot application. How would you ensure the correct order of beans in the injected collection?
- How does the @WebMvcTest annotation in Spring Boot testing differ from @SpringBootTest in terms of loaded context?
- In Spring Boot, to apply validation constraints on a field, the _____ annotation is used along with specific constraint annotations.