Mark As Completed Discussion

Are you sure you're getting this? Fill in the missing part by typing it in.

To create a REST API in Spring Boot, you need to define a controller class and annotate it with @RestController. The @RestController annotation combines the functionality of the @Controller and @ResponseBody annotations, making it easier to develop RESTful APIs.

Here's an example of a UserController class that handles HTTP requests for a User resource:

TEXT/X-JAVA
1@RestController
2@RequestMapping("/api/users")
3public class UserController {
4
5   @Autowired
6   private UserService userService;
7
8   @GetMapping
9   public List<User> getAllUsers() {
10       return userService.getAllUsers();
11   }
12
13   @PostMapping
14   public User createUser(@RequestBody User user) {
15       return userService.createUser(user);
16   }
17
18   @GetMapping("/{id}")
19   public User getUserById(@PathVariable("id") Long id) {
20       return userService.getUserById(id);
21   }
22
23   @PutMapping("/{id}")
24   public User updateUser(@PathVariable("id") Long id, @RequestBody User user) {
25       return userService.updateUser(id, user);
26   }
27
28   @DeleteMapping("/{id}")
29   public void deleteUser(@PathVariable("id") Long id) {
30       userService.deleteUser(id);
31   }
32
33}

Write the missing line below.