This document provides detailed information on how to use the endpoints of a Java API implemented with the following REST Controller.
The RecipesController
class is a REST controller responsible for handling recipe-related operations. It provides endpoints to perform CRUD (Create, Read, Update, Delete) operations on recipes.
Before using the endpoints, ensure that you have the following prerequisites in place:
- Java Development Kit (JDK) 17 installed
- IDE (Integrated Development Environment) installed (e.g., IntelliJ, Eclipse)
- Maven build tool installed
- Database setup and connection details
The following are the available endpoints provided by the RecipesController
:
- Endpoint:
GET /recipes/{id}
- Description: Retrieves a recipe by its ID.
- Request Parameters:
id
(path variable): The ID of the recipe to retrieve.
- Response:
- HTTP Status:
200 OK
if the recipe is found. - JSON object representing the recipe.
- HTTP Status:
- Example:
GET /recipes/1
- Endpoint:
POST /recipes
- Description: Creates a new recipe.
- Request Body:
- JSON object representing the recipe to be created.
- Response:
- HTTP Status:
200 OK
if the recipe is created successfully. - JSON object representing the created recipe.
- HTTP Status:
- Example:
POST /recipes
- Request Body:
{ "title": "Chocolate Cake", "vegetarian": true, "servings": 8, "ingredients": ["flour", "sugar", "cocoa powder", "eggs", "butter"], "instructions": "1. Mix all ingredients..." }
- Endpoint:
PUT /recipes/{id}
- Description: Updates an existing recipe.
- Request Parameters:
id
(path variable): The ID of the recipe to update.
- Request Body:
- JSON object representing the updated recipe.
- Response:
- HTTP Status:
200 OK
if the recipe is updated successfully. - JSON object representing the updated recipe.
- HTTP Status:
- Example:
PUT /recipes/1
- Request Body:
{ "title": "Updated Chocolate Cake", "vegetarian": true, "servings": 10, "ingredients": ["flour", "sugar", "cocoa powder", "eggs", "butter"], "instructions": "1. Mix all ingredients..." }
- Endpoint:
DELETE /recipes/{id}
- Description: Deletes a recipe by its ID.
- Request Parameters:
id
(path variable): The ID of the recipe to delete.
- Response:
- HTTP Status:
204 No Content
if the recipe is deleted successfully.
- HTTP Status:
- Example:
DELETE /recipes/1
- Endpoint:
GET /recipes/filter
- Description: Filters recipes based on specific criteria.
- Request Parameters:
page
(query parameter): The page number (starting from 0) for pagination.size
(query parameter): The number of recipes per page.
- Request Body:
- JSON object containing filtering criteria.
- Response:
- HTTP Status:
200 OK
if there are matching recipes. - JSON object containing the filtered recipes.
- HTTP Status:
- Example:
- `GET /recipes
/filter?page=0&size=10`
- Request Body:
{ "title": "chocolate", "vegetarian": true, "servings": 4, "ingredients": ["cocoa powder", "sugar"], "instructions": ["Mix all ingredients...", "Bake for 30 minutes..."] }
To set up and run the API locally, follow these steps:
- Clone the repository or download the source code.
- Open the project in your preferred IDE.
- Build the project using Maven or Gradle.
- Configure the database connection details in the repository (if required).
- Run the application from the IDE or using the command-line interface.
- The API will be accessible at
http://localhost:8080/recipes
.
The API handles errors in the following scenarios:
- If a recipe with the specified ID is not found, the API returns a
404 Not Found
status. - If an invalid request is made (e.g., missing required fields), the API returns a
400 Bad Request
status. - If an internal server error occurs, the API returns a
500 Internal Server Error
status.
The RecipesController
relies on the following dependencies:
- Spring Boot
- Spring Web
- Spring Data JPA
- Hibernate Validator
Make sure to include these dependencies in your project's pom.xml
or build.gradle
file.
This readme provides an overview of the endpoints available in the RecipesController
of the Java API. By following the instructions and examples provided, you can use these endpoints to perform various operations on recipes.