allthingsare๐Ÿ…ฟ๏ธ.com Books โฌ…๏ธ Back allthingsare

โ€œHe who began a good work in you will carry it on to completion.โ€
[Philippians 1:6]


How Spring Boot Uses Java



What is Spring Boot?

Spring Boot is a popular Java web framework that makes building backends:

  • Fast to start

  • Easy to run

  • Ready for production

It hides a lot of the complex Servlet setup, so you just write controllers, services, repositories, and Spring handles routing, JSON, DB, security.


How your core Java connects:

  • Controllers โ†’ Java methods with @GetMapping, @PostMapping.

  • Services โ†’ Your business logic classes.

  • Models/Entities โ†’ Your User class with ID, name, email.

  • Repositories โ†’ DB access layer, often built with Spring Data JPA.

  • Exception Handling โ†’ @ExceptionHandler for clean error JSON.



Simple Example: Your CRUD as a REST API


Hereโ€™s how the same UserService idea expands to Spring Boot:

java
// User.java (model) public class User { private int id; private String name; private String email; // getters/setters } // UserService.java import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; @Service public class UserService { private List<User> users = new ArrayList<>(); public void addUser(String name, String email) { users.add(new User(users.size() + 1, name, email)); } public List<User> getAllUsers() { return users; } } // UserController.java import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/users") public class UserController { private final UserService userService; // Constructor Injection public UserController(UserService userService) { this.userService = userService; } @PostMapping public void addUser(@RequestBody User user) { userService.addUser(user.getName(), user.getEmail()); } @GetMapping public List<User> getAllUsers() { return userService.getAllUsers(); } }

Key differences vs plain Java:

  • No main() doing manual calls โ€” instead, Spring Boot runs an embedded server (Tomcat) that listens for HTTP requests.

  • @RestController means methods return JSON by default.

  • @RequestMapping("/users") maps the URL to your controller.

  • @PostMapping, @GetMapping handle different HTTP methods.

  • @RequestBody binds incoming JSON to your User class.



20.3 Mini Lab: Run a Tiny Spring Boot CRUD


This is how your plain Java CRUD turns into an actual API.


1๏ธโƒฃ In your IDE, create a Spring Boot project with:

  • Spring Web dependency

  • Spring Boot DevTools (optional, for auto-reload)

2๏ธโƒฃ Place the User, UserService, and UserController in the right packages (model, service, controller).

3๏ธโƒฃ Run your app (DemoApplication.java).

4๏ธโƒฃ Use Postman or curl:

bash
curl -X POST http://localhost:8080/users \ -H "Content-Type: application/json" \ -d '{"name":"Alice","email":"alice@example.com"}' curl http://localhost:8080/users

๐Ÿš€ Congrats:
You just turned core Java โ†’ real RESTful backend โ†’ JSON โ†’ usable by React, Vue, mobile apps!