Building a REST API with Spring Boot
Prerequisites: JDBC & database access
Spring Boot is the dominant framework for building Java backend services — it handles the HTTP server, routing, and dependency injection so a working endpoint is a handful of annotated lines:
@RestController
@RequestMapping("/api/users")
class UserController {
@GetMapping("/{id}")
User getUser(@PathVariable int id) {
return new User(id, "Ada");
}
@PostMapping
User createUser(@RequestBody User user) {
// save user...
return user;
}
}
@RestController marks the class as handling HTTP requests,
@GetMapping/@PostMapping map a method to an HTTP verb and path,
and @RequestBody automatically deserializes an incoming JSON body
into a Java object — Spring wires all of this together via
dependency injection and reflection, both covered earlier in this
level.