Day 7 · 1 Hour
Spring Boot
From 3 days of XML config to 30 seconds — build your first production REST API.
🔥 Real-World Hook (0–5 min)
Before Spring Boot: setting up a Java web server took 3 days of XML config, WAR file deployment, and endless boilerplate.
After Spring Boot: 30 seconds. One annotation and your app is running. Swiggy, Razorpay, Urban Company — all use Spring Boot backends. Today you build your first REST API.
🪝 What Spring Boot Does For You (5–20 min)
💡 Analogy
Raw Java is building a car from scratch — engine, seats, wheels, fuel system. Spring Boot is buying a car — it comes configured. You just turn the key (@SpringBootApplication) and drive.
💻 Live Coding — Student REST API (20–40 min)
// pom.xml dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
// Model public class Student { private Long id; private String name; private String email; private String dept; // constructors + getters + setters } // Controller — maps HTTP requests to methods @RestController @RequestMapping("/api/students") public class StudentController { private List<Student> students = new ArrayList<>(); private Long nextId = 1L; @GetMapping public List<Student> getAll() { return students; } @GetMapping("/" ) public ResponseEntity<Student> getById(@PathVariable Long id) { return students.stream() .filter(s -> s.getId().equals(id)) .findFirst() .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); } @PostMapping public ResponseEntity<Student> create(@RequestBody Student s) { s.setId(nextId++); students.add(s); return ResponseEntity.status(201).body(s); } @DeleteMapping("/" ) public ResponseEntity<Void> delete(@PathVariable Long id) { students.removeIf(s -> s.getId().equals(id)); return ResponseEntity.noContent().build(); } }
🎯 You Code It (40–55 min)
- Create a Spring Boot project at start.spring.io — add "Spring Web" dependency
- Create a
Productclass: id, name, price (double), category - Create
ProductControllerwith: GET all, GET by id, POST create, DELETE - Add GET
/api/products/category/{cat}— return products filtered by category - Test using Postman or
curl http://localhost:8080/api/products - Show: All 5 endpoints working in Postman
📄 Day 7 Cheat Sheet — Spring Boot
Core Annotations
| @SpringBootApplication | Main class |
| @RestController | HTTP endpoints |
| @Service | Business logic |
| @Repository | Data access |
| @Component | Generic bean |
| @Autowired | Inject dependency |
HTTP Mappings
| @GetMapping | GET request |
| @PostMapping | POST request |
| @PutMapping | PUT request |
| @DeleteMapping | DELETE request |
| @RequestMapping("/base") | Base path |
| @PathVariable Long id | URL param |
Request/Response
| @RequestBody T obj | JSON body -> object |
| @RequestParam String x | ?x=val query param |
| ResponseEntity.ok(body) | 200 OK with body |
| ResponseEntity.status(201) | Custom status |
| ResponseEntity.notFound() | 404 |
| ResponseEntity.noContent() | 204 No Content |
application.properties
| server.port=8080 | Change port |
| spring.datasource.url=... | DB URL |
| spring.datasource.username=... | DB user |
| spring.jpa.show-sql=true | Log SQL |
| spring.jpa.hibernate.ddl-auto=update | Auto schema |