A ready-to-use Template for Rest API using spring-boot-microservices, MongoDB as Database, Integrated with codecov and sonarqube, deployable to cloud. This template can be plugged into any application to kickstart task easily without writing everything from scratch.
- Rest controllers and models using SpringBoot
- MongoDB configuration
- TravisCI build
- SonarQube integration
- Jacoco Test report
- JWT authentication
- 80% and above Code Coverage (using codecov or coveralls)
- Cloud deployment
- Import this project into your favourite IDE after fork and checkout of this repository.
- Install Java 17.
- Install Docker and Docker Compose if you want the recommended local MongoDB setup.
- Copy
.env.exampleinto your local environment configuration and set the values for your machine:SERVER_PORTMONGODB_URIJWT_SECRETJWT_EXPIRATION_MS
- Start MongoDB locally with Docker Compose:
docker compose up -d mongodbThis starts MongoDB on localhost:27017 and stores database files in the named mongodb_data Docker volume. The default .env.example value, MONGODB_URI=mongodb://localhost:27017/rest_tutorial, connects the app to this container.
-
If you prefer to manage MongoDB yourself, get a running instance of MongoDB that you can connect to. For more information on getting started with MongoDB, visit their online tutorial.
-
Start by creating a test database. I will call mine "rest_tutorial" using the following command in the MongoDB shell, Docker Compose container, or through a database manager like MongoDB Compass:
use rest_tutorial; -
Create a sample collection that will hold data about different types of pets. Let's create the collection with the following command:
db.createCollection("pets"); -
Once the collection is created, we need to add some data! We can add data to the collection with the below query, you can add any number of data like this :
db.pets.insertMany([{"name" : "Spot","species" : "dog","breed" : "pitbull"},{"name" : "Daisy","species" : "cat","breed" : "calico"},{"name" : "Bella","species" : "dog","breed" : "australian shepard"}]); -
Add the MongoDB connection details through
MONGODB_URI. Keep secrets out of application.properties. -
Create the user roles in the database. The user roles can be one of "USER, MODERATOR or ADMIN"
db.roles.insertMany([
{ name: "ROLE_USER" },
{ name: "ROLE_MODERATOR" },
{ name: "ROLE_ADMIN" },
])
After MongoDB is running and your environment variables are configured, start the API with Gradle:
./gradlew bootRunThe application uses MONGODB_URI=mongodb://localhost:27017/rest_tutorial by default for local Docker Compose development. To stop the MongoDB container when you are done, run:
docker compose downAdd -v to remove the persisted mongodb_data volume as well:
docker compose down -vOnce the server starts, your first need to register a user and login as that user to get a token.\
Once you get the token, you need to pass that token for every request you make to the backend
In the postman, select the "header" section and enter Authorization for the key and
"Bearer " for the value
you are free to test your API however you choose. Use postman for the below tests :
Once done with all the testing, you can logout using the endpoint /api/auth/logout
For code coverage reports integration, I have shown example using Codecov and Coveralls as both are pretty popular and easy to integrate with the travis.
-
Codecov - Just add this line in the .travis.yml which will send the jacoco report to the codecov console
-
Coveralls - Need to add coveralls plugin and jacoco report path in the build.gradle file. Also need change in .travis.yml instead of codecov to use coveralls
A ready-to-copy backlog of suggested GitHub issues is available in docs/github-issue-backlog.md.
Are you up for your first Issue Request for this project !!!
Awesome but please first go through the ISSUE TEMPLATE.md and use this template to submit any issue request.
Are you up for your first PR for this project !!!
Awesome but please first go through the PULL REQUEST TEMPLATE.md and use this template to submit your PR.
Please read CONTRIBUTING.md and CODE OF CONDUCT.md for details on our code of conduct, and the process for submitting pull requests to us.
- Gourav Rusiya
