# Create Rest API with Spring Boot

## **1\. What is Spring Boot?**

* Spring Boot is a framework built on top of the Spring Framework.
    
* It simplifies the process of creating production-ready Spring applications.
    
* Focuses on **convention over configuration**, meaning you need minimal configuration to start.
    
* Provides **embedded servers** (Tomcat, Jetty, Undertow) so you don’t need to deploy WAR files.
    

---

## **2\. Why Spring Boot? (Key Features & Advantages)**

1. **Auto-configuration**
    
    * Automatically configures application components based on classpath and dependencies.
        
2. **Starter Dependencies**
    
    * Predefined dependencies for common use cases (e.g., `spring-boot-starter-web`, `spring-boot-starter-data-jpa`).
        
3. **Embedded Servers**
    
    * No need for external servers like Tomcat; comes embedded.
        
4. **Production-ready**
    
    * Includes Actuator for monitoring, metrics, and health checks.
        
5. **Microservices Friendly**
    
    * Ideal for building REST APIs and microservices.
        
6. **Reduced Boilerplate Code**
    
    * Minimal XML configuration; mostly annotations.
        

---

## **3\. Core Components of Spring Boot**

1. **Spring Boot Starters**
    
    * A set of convenient dependency descriptors to simplify builds.
        
    * Example:
        
        * `spring-boot-starter-web` → For web apps and RESTful services.
            
        * `spring-boot-starter-data-jpa` → For database operations with JPA.
            
2. **Spring Boot Auto-Configuration**
    
    * Configures Spring application automatically based on the libraries in the classpath.
        
    * Controlled via `@SpringBootApplication` annotation (which includes `@EnableAutoConfiguration`).
        
3. **Spring Boot CLI (Optional)**
    
    * Command-line tool to quickly prototype applications using Groovy.
        
4. **Spring Boot Actuator**
    
    * Exposes production-ready features like metrics, health checks, and application info.
        

---

## **4\. Spring Boot Annotations**

* `@SpringBootApplication` – Combines `@Configuration`, `@EnableAutoConfiguration`, and `@ComponentScan`.
    
* `@RestController` – Combines `@Controller` and `@ResponseBody`.
    
* `@GetMapping`, `@PostMapping`, etc. – For handling HTTP requests.
    
* `@Autowired` – For dependency injection.
    
* `@Value` – To inject properties from [`application.properties`](http://application.properties).
    

---

## **5\. How Spring Boot Works (Flow)**

1. Developer creates a main class with `@SpringBootApplication`.
    
2. Application starts using the `main()` method → calls [`SpringApplication.run`](http://SpringApplication.run)`()`.
    
3. Spring Boot scans for components (`@Component`, `@Service`, `@Repository`) and loads them.
    
4. Auto-configuration sets up beans based on the dependencies present.
    
5. Embedded server starts, and the application becomes accessible.
    

# **Spring Boot Syntax Cheat Sheet**

### **Core Annotations**

* `@SpringBootApplication` → Main entry point (enables auto-configuration, component scan, and configuration).
    
* `@RestController` → Combines `@Controller` and `@ResponseBody`.
    
* `@GetMapping`, `@PostMapping`, `@PutMapping`, `@DeleteMapping` → REST endpoints.
    
* `@Autowired` → Dependency Injection.
    
* `@Service` → Business logic layer.
    
* `@Repository` → Database layer (DAO).
    
* `@Entity` → Marks a class as a database table.
    
* `@Table(name = "table_name")` → Optional, for custom table name.
    
* `@Id`, `@GeneratedValue` → Primary key and auto-generation.
    
* `@Value("${`[`property.name`](http://property.name)`}")` → Reads values from [`application.properties`](http://application.properties).
    
* `@Configuration` → Defines configuration class.
    
* `@Bean` → Declares a bean manually.
