Spring Boot Assignment: Post Management System
Objectives:
- Create REST API
- Implement Service Interface and ServiceImpl
- Define Entity class
- Use Spring Data JPA with Hibernate
- Write queries using method names, JPQL, and native SQL
- Apply pagination using Page and Pageable
- Use auditing for automatic createdAt and updatedAt fields
- Organize multiple repositories for different query types
Assignment Requirements:
Develop a REST API for a Post Management System.
1. Entity: Post
- id (Long): primary key
- title (String): required
- content (String)
- author (String): required
- createdAt (LocalDateTime): auto-filled creation timestamp
- updatedAt (LocalDateTime): auto-filled last update timestamp
=> Use Spring Data JPA Auditing: @CreatedDate, @LastModifiedDate, and AuditorAware.
2. Repository
Create multiple repositories:
- PostRepository: contains basic CRUD and method name queries
- PostJqlRepository: contains JPQL queries using @Query
- PostNativeRepository: contains native SQL queries using @Query(nativeQuery = true)
Sample queries:
- Method Name: findByAuthorContainingIgnoreCase(String author, Pageable pageable)
- JPQL: searchByTitle(keyword)
- Native SQL: searchByContent(text)
3. Service
Create PostService interface and PostServiceImpl:
- getAllPosts(Pageable pageable)
- searchByAuthor(...)
- searchByTitle(...)
- searchByContentNative(...)
- getPostById(Long id)
- createPost(Post post)
- updatePost(Long id, Post post)
- deletePost(Long id)
4. REST Controller
Create PostRestController:
- GET /api/posts
- GET /api/posts/{id}
- POST /api/posts
- PUT /api/posts/{id}
- DELETE /api/posts/{id}
- GET /api/posts/search-author?author=...
- GET /api/posts/search-title?keyword=...
- GET /api/posts/search-content?text=...
Technologies:
- Spring Boot 3
- Spring Web