An example to demonstrate API development by using Spring WebFlux in functional style
Before running this project, make sure you have the following prerequisites:
- Java 11 or higher installed
-
Clone the repository:
git clone git@github.com:niteshsinghrajput/spring-boot-webflux-h2.git
-
Navigate to the project directory:
cd spring-boot-webflux-h2
-
Build the project
./gradlew clean build
-
Run the application
java -jar .\build\libs\spring-boot-webflux-h2-0.0.1-SNAPSHOT.jar
Once the application is running, you can access the API endpoints using a tool like Postman or cURL.
- GET
/api/employees
: Get all employees- Sample URL:
http://localhost:8080/api/employees
- Sample URL:
- GET
/api/employees/{id}
: Get an employee by ID- Sample URL:
http://localhost:8080/api/employees/1
- Sample URL:
- POST
/api/employees
: Create a new employee- Sample URL:
http://localhost:8080/api/employees
- Sample Request Payload:
{ "name": "Nitesh Rajput", "email": "nitesh.rajput122@target.com", "salary": 12000 }
- Sample URL:
- PUT
/api/employees/{id}
: Update an existing employee- Sample URL:
http://localhost:8080/api/employees/1
- Sample Request Payload:
{ "name": "Nitesh Rajput", "email": "nitesh.rajput123@target.com", "salary": 12500 }
- Sample URL:
- DELETE
/api/employess/{id}
: Delete an employee- Sample URL:
http://localhost:8080/api/employees/1
- Sample URL:
you can also use curl
to perform CRUD (Create, Read, Update, Delete) operations for the http://localhost:8080/api/employees
endpoint:
-
Create an employee (HTTP POST):
curl -X POST -H "Content-Type: application/json" -d '{"name":"Nitesh Rajput","email": "atnsrajput@gmail.com", "salary": 20000}' http://localhost:8080/api/employees
-
Read All Employees (HTTP GET):
curl http://localhost:8080/api/employees
-
Read Single employee by ID (HTTP GET): Replace
1
with the desired employee ID.curl http://localhost:8080/api/employees/1
-
Update an Employee by ID (HTTP PUT): Replace
1
with the desired employee ID.curl -X PUT -H "Content-Type: application/json" -d '{"name":"Nitesh Rajput","email": "atnsrajput@gmail.com", "salary": 25000}' http://localhost:8080/api/employees/1
-
Delete an Employee by ID (HTTP DELETE): Replace
1
with the desired employee ID.curl -X DELETE http://localhost:8080/api/employees/1
Please note that the above commands is for a JSON-based API and use the -H "Content-Type: application/json"
header for requests that include data. Adjust the commands based on the actual structure of your API and the data format it expects. Also, ensure that your Spring Boot application is running and listening on port 8080 for these commands to work as expected.
Make sure to replace {id} with the actual ID of the employee when using the specific endpoints.
The application configuration can be found in the application.properties file. You can modify the H2 DB connection details, such as the host, port, and database name, in this file.