Spring Boot Fundamentals & REST Controllers
📂 Phase 6: Career Launch — Spring Boot, Projects, Interview Prep (Days 27–30) · JavaSpring Boot is a framework built on top of the core Spring Framework that removes almost all of the manual configuration Spring traditionally required — it gives you sensible defaults, an embedded server, and auto-configuration so you can build a production-ready REST API in minutes instead of hours.
Why Spring Boot Over Plain Spring
| Plain Spring | Spring Boot |
|---|---|
| Manual XML/Java configuration for almost everything | Auto-configuration based on dependencies on the classpath |
| Requires an external server (Tomcat, JBoss) to be installed | Embedded Tomcat server — runs as a standalone JAR with java -jar |
| You manually wire beans | Sensible defaults; override only what you need |
Spring Boot is not a replacement for Spring — it is an opinionated, convention-over-configuration way of using the same underlying Spring Framework.
Inversion of Control (IoC) and Dependency Injection (DI)
Instead of your classes creating their own dependencies with new, the Spring IoC Container creates and manages objects (called beans) and injects them wherever they're needed. This is Dependency Injection — it keeps classes loosely coupled and easy to test.
@Service
public class StudentService {
// Spring creates and injects this automatically
@Autowired
private StudentRepository studentRepository;
}
Creating Your First Spring Boot Project
The fastest way to start is Spring Initializr (start.spring.io) — pick Maven, Java, and the dependencies you need (Spring Web, Spring Data JPA, MySQL Driver), and it generates a ready-to-run project structure.
// Main application class — generated automatically
@SpringBootApplication
public class CareerOsApplication {
public static void main(String[] args) {
SpringApplication.run(CareerOsApplication.class, args);
}
}
@SpringBootApplication is itself a combination of three annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan — it tells Spring Boot to auto-configure beans and scan the current package (and sub-packages) for components.
Building Your First REST Endpoint
@RestController
@RequestMapping("/api/hello")
public class HelloController {
@GetMapping
public String sayHello() {
return "Hello from Spring Boot!";
}
@GetMapping("/{name}")
public String sayHelloTo(@PathVariable String name) {
return "Hello, " + name + "!";
}
}
Key Annotations You Will Use Constantly
| Annotation | Purpose |
|---|---|
| @RestController | Marks a class as a REST API controller; combines @Controller + @ResponseBody so return values are written directly into the HTTP response body |
| @RequestMapping | Maps a base URL path to a controller or method; supports all HTTP methods |
| @GetMapping / @PostMapping / @PutMapping / @DeleteMapping | Shorthand for @RequestMapping restricted to GET, POST, PUT, DELETE |
| @PathVariable | Binds a value from the URL path (e.g. /students/{id}) to a method parameter |
| @RequestParam | Binds a query parameter (e.g. ?page=2) to a method parameter |
| @RequestBody | Binds the JSON request body to a Java object |
@Controller vs @RestController
A plain @Controller is meant for traditional web apps — its methods typically return a view name (like a JSP or Thymeleaf template) for the user to see. @RestController is meant for APIs — it writes the returned object straight into the response body as JSON, with no view rendering involved.
Interview tip: A very common fresher-round question is "What does @SpringBootApplication actually do?" — be ready to break it down into its three component annotations (@Configuration, @EnableAutoConfiguration, @ComponentScan) rather than just saying "it starts the app."