1. Spring Boot Project Setup (using Spring Initializer)
Go to 👉 https://start.spring.io and generate a project with:
-
Project: Maven
-
Language: Java
-
Spring Boot: 3.2 (or latest stable)
-
Dependencies:
-
Spring Web
-
Spring Data JPA
-
H2 Database
-
Spring Boot Actuator
-
Click “Generate”, then unzip and open it in IntelliJ IDEA, VS Code, or Eclipse.
📁 2. Project Structure Breakdown
✅ 3. Step-by-Step File Explanation
📌 CustomerServiceApplication.java
🔹 Explanation:
-
@SpringBootApplication= Combines 3 annotations:-
@Configuration(declares beans) -
@EnableAutoConfiguration(auto sets up Spring features) -
@ComponentScan(scans your code for components like@Controller)
-
-
main()= the entry point. Runs the app like a regular Java program.
📌 Customer.java (inside model/)
🔹 Explanation:
-
This is a Java class (POJO) that represents a table in the database.
-
@Entitytells Spring it's a database table. -
@Id= primary key,@GeneratedValue= auto-increment. -
When the app runs, Spring Boot creates a table named
customerin the H2 DB.
📌 CustomerRepository.java (inside repository/)
🔹 Explanation:
-
This is an interface — no code inside, yet it works!
-
JpaRepositoryprovides built-in methods like:-
findAll() -
save() -
deleteById() -
findById()
-
-
Spring will generate the implementation automatically.
📌 CustomerController.java (inside controller/)
🔹 Explanation:
-
@RestController= this class handles HTTP requests and returns data. -
@RequestMapping("/api/customers")= base path for all endpoints. -
@Autowired= Spring injects the repository automatically. -
@GetMapping= handlesGET /api/customers(return all customers). -
@PostMapping= handlesPOST /api/customers(create a new customer). -
@RequestBody= converts incoming JSON to aCustomerobject.
📌 application.yml
🔹 Explanation:
-
Tells Spring Boot to:
-
Use port 8080
-
Use in-memory H2 DB
-
Automatically update the schema from entities
-
Expose health, metrics, and other actuator endpoints
-
▶️ 4. Running the App
-
Run
CustomerServiceApplication.java(as Java Application) -
Test in browser or with curl/Postman:
🐳 5. Dockerizing the App
Step-by-step:
🐘 Build the project
This creates a file like target/customer-service-0.0.1-SNAPSHOT.jar.
🐳 Dockerfile
🛠 Build and Run Docker
Now your app is running in a container!
🚀 6. CI/CD with GitHub Actions
Here’s a simple GitHub Actions pipeline:
.github/workflows/build.yml
🛠️ 7. Debugging Tips
🔧 Common errors and how to fix:
| Problem | Symptom | Fix |
|---|---|---|
| Port in use | App fails to start | Change server.port |
| H2 DB error | Cannot connect | Fix DB URL or check logs |
| NullPointer | Missing @Autowired | Add annotation |
| App not accessible in Docker | No port exposed | Use -p 8080:8080 in Docker run |
✅ Summary
| Task | Tool |
|---|---|
| Build app | Spring Boot + Maven |
| Test locally | Curl/Postman |
| Deploy in container | Docker |
| Automate | GitHub Actions |
| Monitor health | Actuator /actuator/health |
| Debug | Logs, Postman, Spring DevTools |
No comments:
Post a Comment