This project is a RESTful API for a personal blogging platform built with Spring Boot and MongoDB. The API allows users to perform basic CRUD (Create, Read, Update, Delete) operations for managing blog posts. Users can create, update, delete, retrieve, and search blog posts based on a term.
- Create a Blog Post: Add a new blog post with title, content, category, and tags.
- Retrieve Blog Posts: Retrieve all blog posts or filter posts by a search term.
- Update a Blog Post: Modify the details of an existing blog post.
- Delete a Blog Post: Remove a blog post by ID.
- Search: Use a search term to filter posts based on title, content, or category.
- Java 21
- MongoDB (URI and database required)
- Maven (for building the project)
-
Clone the repository:
git clone https://github.com/your-username/blog-platform.git cd blog-platform
-
Install dependencies and build the project:
mvn clean install
Set up the following properties in src/main/resources/application.properties
:
# Application name
spring.application.name=blog-platform
# MongoDB Configuration
spring.data.mongodb.uri=YOUR_MONGODB_URI
spring.data.mongodb.database=blogplatform
- spring.application.name: The name of your application.
- spring.data.mongodb.uri: The MongoDB connection URI.
- spring.data.mongodb.database: The name of the MongoDB database to use.
After configuration, start the application with:
mvn spring-boot:run
The API should now be accessible at http://localhost:8080
.
Method | Endpoint | Description |
---|---|---|
- | / |
Swagger UI |
GET | /health |
Check the health of application |
POST | /posts |
Create a new blog post |
GET | /posts |
Get all blog posts |
GET | /posts/{postId} |
Get a single blog post by ID |
PUT | /posts/{postId} |
Update a blog post by ID |
DELETE | /posts/{postId} |
Delete a blog post by ID |
GET | /posts?term={term} |
Filter blog posts by a search term |
Create Blog Post
POST /posts
Content-Type: application/json
{
"title": "My First Blog Post",
"content": "This is the content of my first blog post.",
"category": "Technology",
"tags": ["Tech", "Programming"]
}
Get All Blog Posts
GET /posts
Get a Single Blog Post
GET /posts/1
Update a Blog Post
PUT /posts/1
Content-Type: application/json
{
"title": "My Updated Blog Post",
"content": "This is the updated content of my first blog post.",
"category": "Technology",
"tags": ["Tech", "Programming"]
}
Delete a Blog Post
DELETE /posts/1
Filter Blog Posts
GET /posts?term=tech
- 201 Created: Resource successfully created.
- 200 OK: Request was successful.
- 204 No Content: Resource successfully deleted.
- 400 Bad Request: Validation failed.
- 404 Not Found: Resource not found.
You can tweak the following constants in BlogPostConstants.java
to adjust database and search settings:
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class BlogPostConstants {
// Change these according to your database, collection, and search index
public static final String BLOG_DATABASE = "blogplatform"; // Database name
public static final String BLOG_POST_COLLECTION = "blog"; // Collection name
public static final String SEARCH_INDEX_NAME = "search_term"; // Index name for search
public static final String CREATED_AT = "createdAt";
public static final String CONTENT = "content";
public static final String TITLE = "title";
public static final String CATEGORY = "category";
public static final String TAGS = "tags";
// Sorting order: -1 for descending, 1 for ascending; field is 'createdAt'
public static final Integer SORT_ORDER = -1;
public static final Integer BLOG_SEARCH_LIMIT = 5;
public static final String SEARCH_AGGREGATION = "$search";
public static final String SORT_AGGREGATION = "$sort";
public static final String LIMIT_AGGREGATION = "$limit";
public static final String INDEX = "index";
public static final String TEXT = "text";
public static final String QUERY = "query";
public static final String PATH = "path";
}
-
Database and Collection Names:
BLOG_DATABASE
: Change this to match your MongoDB database name.BLOG_POST_COLLECTION
: Set this to your MongoDB collection name for blog posts.- Ensure these names match the ones you've set in your MongoDB setup.
-
Search Index Name:
SEARCH_INDEX_NAME
: Set this to the name of your MongoDB Atlas search index.- This is crucial for the search functionality to work correctly.
- Link to setup search index.
-
Sorting Order:
SORT_ORDER
: Adjust to-1
for descending or1
for ascending order based on thecreatedAt
field.- This changes the order in which blog posts are returned when fetching multiple posts.
-
Search Limit:
BLOG_SEARCH_LIMIT
: Change this value to increase or decrease the number of search results returned.- Useful for pagination or limiting the amount of data returned in a single request.
- Java 21: Core programming language.
- Spring Boot: Framework for building the RESTful API.
- MongoDB: NoSQL database for storing blog posts.
- Maven: Dependency management and build tool.
This project is licensed under the MIT License - see the LICENSE file for details.
For any questions or issues, please contact Shivam.