Spring Boot Basics
Spring Boot makes Java web development fast by auto-configuring everything. You focus on writing business logic — Spring handles the boring setup. Let's build your first REST API.
Spring Boot is like buying furniture fully assembled and ready to use — it comes with smart defaults (auto-configuration) so you can start immediately. And if you don't like a default, you can override it.
| Spring Framework | Spring Boot |
|---|---|
| Manual XML/Java configuration | Auto-configuration with sensible defaults |
| Need external Tomcat server | Embedded Tomcat — run with java -jar |
| Complex dependency management | Starter POMs bundle compatible versions |
| Verbose setup | Production-ready in minutes |
Install the Extension
In VS Code, open Extensions (Ctrl+Shift+X) and install Spring Boot Extension Pack by VMware. This includes the Spring Initializr Java Support extension.
Open the Command Palette
Press Ctrl+Shift+P → type Spring Initializr: Create a Maven Project → press Enter.
Answer the prompts
VS Code will ask you each field one at a time in the top bar:
- Spring Boot version → 3.x.x
- Group ID → com.campustoai
- Artifact ID → student-api
- Packaging → Jar
- Java version → 21
Select Dependencies
A multi-select list appears. Search and select:
- Spring Web
- Spring Boot DevTools
- Lombok
Press Enter when done.
Choose folder & open
VS Code asks where to save the project. Pick a folder → click Generate into this folder. The project opens automatically — no zip, no extract.
Open the generator
Go to start.spring.io in any browser. You will see a form with several fields — here is what each one means.
Project — Build tool
Choose the tool that compiles and packages your code.
| Option | What it is | When to use |
|---|---|---|
| Maven | Uses pom.xml. Very common in Indian tech companies. | Use this for this course |
| Gradle | Uses build.gradle. Faster builds, popular in Android. | Skip for now |
Group ID — Your organisation's namespace
Group ID uniquely identifies your organisation or project family across all Maven projects in the world. It is written in reverse domain-name format.
com.zomato means "a project by Zomato". It prevents name collisions — two different companies can both have a project called user-service, but their Group IDs are different.
| Company / Domain | Group ID |
|---|---|
| campustoai.com | com.campustoai |
| zomato.com | com.zomato |
| flipkart.in | in.flipkart |
| Your startup | com.yourstartup |
Group ID also becomes the root Java package for your code. So com.campustoai means your Java files will live under com/campustoai/.
com.company or com.company.team.Artifact — Your project's name
Artifact is the name of this specific project or microservice. It becomes the name of the final JAR file that gets built.
com.amazon:delivery-service.
| Artifact value | JAR file produced |
|---|---|
student-api | student-api-0.0.1-SNAPSHOT.jar |
payment-service | payment-service-0.0.1-SNAPSHOT.jar |
auth-service | auth-service-0.0.1-SNAPSHOT.jar |
Name & Description
Name is auto-filled from Artifact. It becomes the main class name (e.g., artifact student-api → class StudentApiApplication). You can leave it as-is.
Description is a plain English sentence about your project. It only appears in pom.xml as a comment — no functional impact.
Packaging — How your app is bundled
Packaging decides the format of the file Maven builds when you run mvn package.
| Option | File produced | How to run | When to use |
|---|---|---|---|
| Jar ✅ | .jar (Java Archive) |
java -jar app.jarTomcat is embedded inside the jar |
Modern Spring Boot apps, microservices, Docker containers, cloud deployments. Always use this. |
| War | .war (Web Archive) |
Deploy to an external Tomcat or JBoss server | Legacy enterprise apps that already have a corporate Tomcat server. Avoid unless required. |
Java Version
Select 21 (LTS — Long Term Support). Spring Boot 3.x requires Java 17 minimum. Java 21 is the latest LTS release and what this course uses.
Dependencies — What your project needs
Click ADD DEPENDENCIES and search for:
This is the core dependency for any REST API. Without it, your Spring Boot app has no web server and cannot handle HTTP requests.
It bundles three things for you automatically:
• Spring MVC — the framework that maps HTTP requests to your @GetMapping / @PostMapping methods
• Embedded Tomcat — the web server. No separate Tomcat installation needed; it starts inside your JAR
• Jackson — the JSON library. Converts your Java objects ↔ JSON automatically. When you return a
Student object from a controller, Jackson turns it into {"id":1,"name":"Rahul"}
A development-only helper that monitors your project files. Every time you save a .java file, it automatically restarts the Spring context (in ~1 second instead of restarting the whole JVM which takes 4–5 seconds).
• Without DevTools: Change code → stop server manually → run again → wait 5 sec
• With DevTools: Change code → save → app reloads in 1 sec automatically
Note: DevTools is automatically excluded from production builds — it only activates during development.
Java classes require a lot of repetitive code — getters, setters, constructors, equals, hashCode, toString. Lombok generates all of this at compile time using annotations, so you don't write it.
Without Lombok (you write ~30 lines):
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
// ... same for every field
With Lombok (you write 3 lines):
@Data // generates ALL getters, setters, equals, hashCode, toString
@AllArgsConstructor // generates constructor with all fields
@NoArgsConstructor // generates empty constructor (needed for JSON)
public class Student { private String name; private String email; }
Generate & Open
Click the GENERATE button (or press Ctrl+Enter). A .zip file downloads. Extract it to a folder, then open that folder in VS Code with File → Open Folder.
Project: Maven | Language: Java | Spring Boot: 3.x.x
Group: com.campustoai | Artifact: student-api
Packaging: Jar | Java: 21
| File / Folder | Purpose |
|---|---|
StudentApiApplication.java | Main class with main() method. Starts the Spring app and embedded Tomcat. |
application.properties | Configuration: server port, database URL, log level, etc. |
pom.xml | Dependencies (Spring Web, JPA, etc.) and build config. |
src/test/ | Unit and integration tests. |
new), the restaurant manager (Spring IoC Container) provides the knife. The chef just declares "I need a knife" and it appears.This is Inversion of Control (IoC) — you give control of object creation to the Spring container, not your code.
Without DI (bad — tight coupling)
With DI (good — loose coupling)
@Component (generic), @Service (business logic), @Repository (data access), @Controller/@RestController (web layer). All are detected by @ComponentScan.Run from terminal
Spring Boot starts an embedded Tomcat server. Look for this line in the logs:
DevTools — Auto-restart on save
With spring-boot-devtools in pom.xml, every time you save a file, Spring Boot automatically restarts (in ~1 second instead of 3-4 seconds). Enable it in VS Code's Java settings.
Common Annotations Reference
| Annotation | Purpose |
|---|---|
@SpringBootApplication | Main class — enables auto-config + component scan |
@RestController | Class handles HTTP requests, returns JSON |
@RequestMapping | Maps URL path to a class or method |
@GetMapping | HTTP GET request handler |
@PostMapping | HTTP POST request handler |
@PutMapping | HTTP PUT request handler |
@DeleteMapping | HTTP DELETE request handler |
@PathVariable | Extract value from URL path: /students/{id} |
@RequestBody | Map JSON request body to Java object |
@RequestParam | Extract query param: /students?page=2 |
@Service | Business logic layer bean |
@Autowired | Inject a dependency (prefer constructor injection) |
Spring Boot is an opinionated framework built on top of Spring Framework that simplifies application setup. Spring Framework is flexible but requires extensive XML/Java configuration. Spring Boot provides:
- Auto-configuration — detects libraries on classpath and configures them automatically
- Embedded server — Tomcat/Jetty included, no separate installation needed
- Starter POMs — curated dependency bundles (spring-boot-starter-web includes Spring MVC + Jackson + Tomcat)
- Actuator — production-ready monitoring endpoints out of the box
It's a convenience annotation that combines three annotations:
@Configuration— marks the class as a source of Spring bean definitions@EnableAutoConfiguration— tells Spring Boot to auto-configure beans based on the classpath@ComponentScan— scans the package (and sub-packages) for @Component, @Service, @Controller, @Repository beans
Dependency Injection is a design pattern where an object receives its dependencies from outside rather than creating them. Spring manages object creation and wiring.
Three types:
- Constructor injection (recommended) — dependencies passed via constructor, supports final fields, makes testing easy
- Setter injection — dependencies set via setter methods, allows optional dependencies
- Field injection (
@Autowiredon field) — convenient but makes testing harder, not recommended for production
@Controller is used for traditional MVC web applications where methods return view names (HTML templates like Thymeleaf). The return value is resolved to a view by ViewResolver.
@RestController = @Controller + @ResponseBody. Every method's return value is automatically serialised to JSON (or XML) and written directly to the HTTP response. Used for building REST APIs.
@RequestBody maps the HTTP request body (JSON/XML) to a Java object. Used with POST/PUT. Jackson library converts JSON → Java object automatically.
@PathVariable extracts a value from the URL path. For @GetMapping("/students/{id}"), the {id} in the URL is extracted with @PathVariable Long id.
@RequestParam extracts query parameters from the URL: /students?page=2&size=10.
IoC is a principle where the control of object creation and dependency wiring is given to a framework/container instead of the application code. In traditional programming, your code calls a library. With IoC, the framework calls your code.
Spring's IoC Container (ApplicationContext) creates beans, manages their lifecycle, and injects dependencies. You just annotate your classes and Spring does the rest.