Spring Boot 3.x · Java 17+

Spring Boot Tutorial
for Beginners (2026)

Learn Spring Boot from scratch — REST APIs, JPA, Security, and deployment — with real code examples. Written by Deen Bandhu, working Java developer.

⏰ 25 min read 🎥 Hands-on examples ✅ Spring Boot 3.x 🏭 Production-ready patterns

1. What is Spring Boot?

Spring Boot is an opinionated framework built on top of Spring Framework that eliminates boilerplate configuration. It gives you a production-ready application in minutes — embedded Tomcat server, auto-configuration, and starter dependencies do the heavy lifting.

Auto-Configuration
Detects what's on the classpath and configures beans automatically — no XML.
📦
Starter POMs
One dependency pulls everything needed — spring-boot-starter-web includes Tomcat, MVC, Jackson.
🏭
Embedded Server
Tomcat/Jetty/Undertow bundled inside the JAR. Run with java -jar app.jar.
📈
Actuator
Built-in endpoints for health checks, metrics, and monitoring.

2. Project Setup

Use Spring Initializr at start.spring.io. Select:

  • Project: Maven
  • Language: Java
  • Spring Boot: 3.2.x
  • Java: 17
  • Dependencies: Spring Web, Spring Data JPA, MySQL Driver, Validation, Lombok
pom.xml — key dependencies
<!-- Web + REST API -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- JPA + Hibernate -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- MySQL -->
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>

<!-- Lombok (boilerplate reduction) -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

3. Application Layers

Spring Boot applications follow a layered architecture:

C
Controller Layer @RestController
Handles HTTP requests, validates input, returns responses. Should NOT contain business logic.
S
Service Layer @Service
Contains all business logic. Orchestrates between controller and repository. Where transactions live.
R
Repository Layer @Repository
Data access only — CRUD operations, JPA queries. Extends JpaRepository.
E
Entity Layer @Entity
JPA entities map to database tables. Use DTOs between controller and service — never expose entities directly.

4. Building a REST API

ProductController.java
@RestController
@RequestMapping("/api/products")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService productService;

    @GetMapping
    public ResponseEntity<List<ProductDTO>> getAll() {
        return ResponseEntity.ok(productService.findAll());
    }

    @GetMapping("/")
    public ResponseEntity<ProductDTO> getById(@PathVariable Long id) {
        return ResponseEntity.ok(productService.findById(id));
    }

    @PostMapping
    public ResponseEntity<ProductDTO> create(
            @Valid @RequestBody ProductDTO dto) {
        ProductDTO saved = productService.save(dto);
        return ResponseEntity.status(201).body(saved);
    }

    @PutMapping("/")
    public ResponseEntity<ProductDTO> update(
            @PathVariable Long id,
            @Valid @RequestBody ProductDTO dto) {
        return ResponseEntity.ok(productService.update(id, dto));
    }

    @DeleteMapping("/")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        productService.delete(id);
        return ResponseEntity.noContent().build();
    }
}

5. Spring Data JPA

Product.java — Entity
@Entity
@Table(name = "products")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 100)
    private String name;

    @Column(nullable = false)
    private Double price;

    @Column(length = 500)
    private String description;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id")
    private Category category;
}
ProductRepository.java
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {

    // Spring Data auto-generates query from method name
    List<Product> findByNameContainingIgnoreCase(String keyword);
    List<Product> findByCategoryIdOrderByPriceAsc(Long categoryId);

    // Custom JPQL query
    @Query("SELECT p FROM Product p WHERE p.price BETWEEN :min AND :max")
    List<Product> findInPriceRange(@Param("min") Double min,
                                    @Param("max") Double max);
}

6. Validation

ProductDTO.java
@Data
public class ProductDTO {

    @NotBlank(message = "Name is required")
    @Size(min = 2, max = 100, message = "Name must be 2-100 characters")
    private String name;

    @NotNull(message = "Price is required")
    @DecimalMin(value = "0.01", message = "Price must be positive")
    private Double price;

    @Size(max = 500)
    private String description;
}

7. Global Exception Handling

GlobalExceptionHandler.java
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<ErrorResponse> handleNotFound(
            ResourceNotFoundException ex) {
        return ResponseEntity.status(404)
            .body(new ErrorResponse("NOT_FOUND", ex.getMessage()));
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<ErrorResponse> handleValidation(
            MethodArgumentNotValidException ex) {
        Map<String, String> errors = ex.getBindingResult()
            .getFieldErrors().stream()
            .collect(Collectors.toMap(
                FieldError::getField,
                FieldError::getDefaultMessage));
        return ResponseEntity.badRequest()
            .body(new ErrorResponse("VALIDATION_FAILED", errors.toString()));
    }
}

8. Spring Security Basics

SecurityConfig.java (JWT setup skeleton)
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {

    private final JwtAuthFilter jwtAuthFilter;

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        return http
            .csrf(AbstractHttpConfigurer::disable)
            .authorizeHttpRequests(auth -> auth
                .requestMatchers("/api/auth/**").permitAll()
                .anyRequest().authenticated()
            )
            .sessionManagement(s -> s
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS))
            .addFilterBefore(jwtAuthFilter,
                UsernamePasswordAuthenticationFilter.class)
            .build();
    }
}

9. application.properties

src/main/resources/application.properties
# Server
server.port=8080
spring.application.name=my-app

# Database (MySQL)
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# JPA / Hibernate
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect

# Logging
logging.level.org.springframework=INFO
logging.level.com.yourpackage=DEBUG

10. Deployment

01
Build JAR mvn clean package
Run mvn clean package -DskipTests. Creates target/app.jar with embedded Tomcat.
02
Run locally java -jar
java -jar target/app.jar --spring.profiles.active=prod. Use profiles for environment-specific configs.
03
Deploy to Cloud
AWS EC2 / Elastic Beanstalk, Railway, Render, or Heroku. Copy JAR, set environment variables (DB URL, passwords), run with systemd or Docker.
04
Dockerize
FROM eclipse-temurin:17-jre, COPY target/app.jar app.jar, ENTRYPOINT ["java","-jar","app.jar"]. Build and push to Docker Hub or ECR.
Spring Boot 3REST APISpring Data JPA Spring SecurityJava 17MySQLHibernate
AlgoVentra — Spring Boot Course

Build Real Spring Boot Projects Live

Deen Bandhu's course covers everything above — with live coding, real projects (e-commerce API, JWT auth), code reviews, and placement guidance.