A comprehensive Spring Boot tutorial application demonstrating REST API development, JPA relationships, and modern Spring Boot best practices. This application serves as a learning resource for understanding Spring Boot concepts through a practical blog system with authors, posts, and comments.
- Author Management: Create and retrieve authors with full name and email
- Post Management: Create and retrieve blog posts with title, content, and excerpts
- Comment System: Add comments to posts with nested resource patterns
- RESTful API: Complete REST API with proper HTTP methods and status codes
- Database Integration: JPA/Hibernate with automatic schema generation
- Layered Architecture: Controllers, Services, Repositories, and Entities
- Data Transfer Objects (DTOs): Clean separation between API and domain layers
- Exception Handling: Global exception handling with proper HTTP status codes
- Object Mapping: MapStruct for type-safe object transformations
- Performance Optimization: @EntityGraph for N+1 query prevention
- Dependency Injection: Spring's IoC container with @Autowired and @Qualifier
- Spring Boot 3.x: Main framework
- Spring Data JPA: Data access layer
- H2 Database: In-memory database for development
- MapStruct: Object mapping library
- Thymeleaf: Template engine for web views
- Maven: Build tool and dependency management
src/main/java/com/example/
βββ PostApplication.java # Main application class
βββ AppController.java # Web controller for main page
βββ GlobalExceptionHandler.java # Global exception handling
βββ NotFoundEntityException.java # Base exception class
βββ author/ # Author domain
β βββ Author.java # JPA entity
β βββ AuthorRepository.java # Spring Data repository
β βββ AuthorService.java # Business logic
β βββ AuthorController.java # REST controller
β βββ AuthorMapper.java # Object mapper
β βββ CreateAuthorRequest.java # Input DTO
β βββ AuthorResponse.java # Output DTO
β βββ AuthorNotFoundException.java # Specific exception
βββ post/ # Post domain
β βββ Post.java # JPA entity with relationships
β βββ PostRepository.java # Repository with @EntityGraph
β βββ PostService.java # Service with cross-service collaboration
β βββ PostController.java # REST controller
β βββ PostMapper.java # MapStruct mapper
β βββ PostDetailMapper.java # Specialized mapper
β βββ CreatePostRequest.java # Input DTO with relationships
β βββ PostResponse.java # Basic output DTO
β βββ PostDetailResponse.java # Detailed output DTO
β βββ PostNotFoundException.java # Specific exception
βββ post/comment/ # Comment domain
βββ Comment.java # JPA entity with lazy loading
βββ CommentRepository.java # Repository
βββ CommentService.java # Service
βββ CommentController.java # REST controller (nested resources)
βββ CommentMapper.java # Object mapper
βββ CreateCommentRequest.java # Input DTO
βββ CommentResponse.java # Output DTO
- Java 17 or higher
- Maven 3.6 or higher
- IDE (IntelliJ IDEA, Eclipse, or VS Code recommended)
git clone <repository-url>
cd springboot-tutorialmvn clean installjava -jar target/post-0.0.1-SNAPSHOT.jarThe application will start on http://localhost:8080
GET /api/authors- Get all authorsGET /api/authors/{id}- Get author by IDPOST /api/authors- Create a new author
GET /api/posts- Get all posts (basic info)GET /api/posts/{id}- Get post by ID (with comments)POST /api/posts- Create a new post
POST /api/posts/{postId}/comments- Create a comment for a post
curl -X POST http://localhost:8080/api/authors \
-H "Content-Type: application/json" \
-d '{
"firstName": "John",
"lastName": "Doe",
"email": "john.doe@example.com"
}'curl -X POST http://localhost:8080/api/posts \
-H "Content-Type: application/json" \
-d '{
"title": "My First Blog Post",
"content": "This is the content of my first blog post...",
"excerpt": "A brief summary of the post content",
"authorId": 1
}'curl -X POST http://localhost:8080/api/posts/1/comments \
-H "Content-Type: application/json" \
-d '{
"text": "This is a great post! Thanks for sharing."
}'curl http://localhost:8080/api/posts/1- Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan
- Entry point for the Spring Boot application
@Entity: Marks classes as JPA entities@Id&@GeneratedValue: Primary key configuration@OneToMany&@ManyToOne: Relationship mapping@JoinColumn: Foreign key specification
JpaRepository: Automatic CRUD operations@EntityGraph: Performance optimization- Query method generation
@RestController: JSON response handling@RequestMapping: URL mapping@GetMapping&@PostMapping: HTTP method mapping@PathVariable&@RequestBody: Parameter binding
@Autowired: Automatic dependency injection@Qualifier: Bean qualification@Service,@Repository,@Controller: Component stereotypes
@ControllerAdvice: Global exception handling@ExceptionHandler: Specific exception handling- Custom exception hierarchy
- MapStruct for type-safe object transformations
@Mapperand@Mappingannotations- Multi-parameter mapping
The application uses H2 in-memory database by default. The database schema is automatically generated from JPA entities:
- author: Authors with id, first_name, last_name, email
- post: Posts with id, title, content, excerpt, author_id (FK)
- comment: Comments with id, text, post_id (FK)
- Author β Post: One-to-Many (one author can have many posts)
- Post β Comment: One-to-Many (one post can have many comments)
The application uses src/main/resources/application.properties for configuration:
# Database Configuration
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
## π Learning Objectives
This tutorial application demonstrates:
1. **Spring Boot Fundamentals**
- Auto-configuration
- Component scanning
- Dependency injection
2. **JPA and Database Integration**
- Entity mapping
- Relationship management
- Repository pattern
3. **REST API Development**
- RESTful design principles
- HTTP method mapping
- Request/response handling
4. **Architecture Patterns**
- Layered architecture
- DTO pattern
- Service layer pattern
5. **Exception Handling**
- Global exception handling
- Custom exceptions
- HTTP status code mapping
6. **Performance Optimization**
- Lazy loading
- Eager loading with @EntityGraph
- N+1 query prevention