A light-weight RESTful API server starter app built with Java Spring Reactive Framework, MongoDB, WebFlux and Gradle.
- Containerised App with Docker
- NoSQL DB - MongoDB
- Metrics exposed. Easy intergration with prometheus and Grafana.
- SQL Version - Coming soon.
Using docker-compose
run both mongoDB and the app as docker containers.
docker-compose up
For this you will need to set up the MongoDB Locally on your machine(Refer the Local MongoDB Setup section towards the end.).
Add the configurations in application.properties
and run the below or use your fav IDE:
./gradlew clean build
./gradlew bootRun
Once the server is running try to access the below APIs using cURL
or POSTMAN
or any tool that you are already using.
CREATE:
POST
/api/add
curl -X POST -d '{"name":"Giri Jeedigunta","email":"[email protected]"}' -H "Content-Type: application/json" http://localhost:4000/api/add -v
READ:
GET
/api/get
curl http://localhost:4000/api/get
GET
/api/get/{email}
curl http://localhost:4000/api/get/{email}
UPDATE:
POST
/api/udpate/{id}
curl -X POST -d '{"name":"Giri J","email":"[email protected]"}' -H "Content-Type: application/json" http://localhost:4000/api/update/5f85c2c8b0abe7062019dd16
DELETE:
POST
/api/delete/{id}
curl -X POST http://localhost:4000/api/delete/5f85c2c8b0abe7062019dd16
If you are not familiar with docker. Follow these steps to setup MongoDB on your machine locally. Here I have instructions for Mac OS.
brew tap mongodb/brew
brew install [email protected]
brew services start [email protected]
or
mongod --config /usr/local/etc/mongod.conf --fork
Use the below section to create user, db, collections etc,.
use admin;
db.createUser({
user: "giri",
pwd: "spring2020",
roles:[
{role: "dbAdmin", db:"springapi"}
]
});
use springapi;
db.createCollection( "api", {
validator: { $jsonSchema: {
bsonType: "object",
required: [ "name" ],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
email: {
bsonType : "string",
description: "must be a string and match the regular expression pattern"
}
}
} }
} );
db.api.insert({"name":"Giri Jeedigunta","email":"[email protected]"});
Add all the MongoDB related config in a simple URI format like below in the application.properties
file.
spring.data.mongodb.uri=mongodb://<Add_Your_UserName>:<Your_Password>@localhost:27017/<DB_NAME>?authSource=admin
// Example:
spring.data.mongodb.uri=mongodb://giri:spring2020@localhost:27017/springapi?authSource=admin