Day 11 · 1 Hour
Full-Stack Integration
Wire frontend to backend — from a collection of code to a working product.
🔥 Real-World Hook (0–5 min)
You have a beautiful frontend. You have a powerful Spring Boot API. They're not talking to each other yet.
This is what separates a collection of code from a working product. Today you wire them together: React/HTML frontend → Fetch API → Spring Boot controller → JPA → MySQL. Real full-stack.
🔗 The Full-Stack Data Flow (5–20 min)
💡 Flow Diagram
👤 User fills form → 🖥 JavaScript fetch() → 🚀 Spring Boot Controller → 📧 Service → 🚬 JPA Repository → 📊 MySQL Database
Response flows back the same way: Database row → Java object → JSON → Browser renders it.
💻 Live Coding — Fix CORS + Wire Frontend (20–40 min)
// CORS config — allow frontend to call your API @Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("http://localhost:3000", "http://localhost:5500") .allowedMethods("GET", "POST", "PUT", "DELETE") .allowedHeaders("*"); } }
// Full-stack: frontend script calling real API const API_BASE = 'http://localhost:8080/api'; async function loadDashboard() { const [students, courses] = await Promise.all([ fetch(`${API_BASE}/students`).then(r => r.json()), fetch(`${API_BASE}/courses`).then(r => r.json()) ]); document.getElementById('student-count').textContent = students.length; document.getElementById('course-count').textContent = courses.length; document.getElementById('student-list').innerHTML = students.slice(0, 5).map(s => ` <tr> <td>${s.name}</td> <td>${s.dept}</td> <td><span class="badge">${s.gpa}</span></td> </tr> `).join(''); } loadDashboard();
🎯 You Code It (40–55 min)
- Add the
CorsConfigclass to your Spring Boot project - Restart the backend — confirm it runs on port 8080
- Create
dashboard.htmlwith 3 sections: stats cards, student table, add-student form - Load stats (total students, average GPA) by calling your API
- The form should POST and instantly refresh the table without page reload
- Show: Dashboard loading real data from your MySQL via Spring Boot
📄 Day 11 Cheat Sheet — Full-Stack
CORS Fix
| @CrossOrigin("*") | Quick fix on controller |
| WebMvcConfigurer | Global CORS config |
| allowedOrigins("url") | Allowed frontend |
| allowedMethods("GET","POST"...) | HTTP verbs allowed |
Parallel Fetch
| Promise.all([...]) | Run all in parallel |
| await Promise.all([f1,f2]) | Wait for all |
| const [a,b] = await... | Destructure results |
| Promise.allSettled | Get all even if some fail |
Debugging Tools
| F12 -> Network tab | See API calls |
| F12 -> Console | JS errors |
| Postman | Test API directly |
| spring.jpa.show-sql=true | See SQL queries |
| console.log(data) | Debug JS variables |
Layered Architecture
| Controller | HTTP in/out, no logic |
| Service | Business rules here |
| Repository | Database access only |
| Entity | Database table model |
| DTO | What client sees |