This is a simple Demo of using Spring Boot
and MongoDB
. Application has a simple form on the main page.
Entered data on the form will be saved users
collection in MongoDB
.
Application consists of:
UserController
which handles request fromindex.html
pageUserResource
- isREST
endpoint for retrievingUsers
UserRepository
- repository interface for persistence and retrievingUsers
fromMongoDB
Because it's a simple demo for storing and retrieving users to/from database, UserRepository
injected directly in
UserController
and UserResource
.
To build, or run project java
and MongoDB
should be installed.
From the root folder of the project just run ./gradlew build
Compiled springboot-mongo-demo.jar
file can be found at build/libs
- To run project from the built
.jar
file:
From the root folder of the project run the following command in terminal:
java -jar build/libs/springboot-mongo-demo.jar
- To run the project with gradle:
From the root folder of the project run the following command in terminal:
gradlew bootRun
- To run the project from the Intellij Idea:
Open the project in Intellij Idea
Open DemoApp.java
file. Right click on the main
method and select Run 'DemoApp.main()'
on the pop-up window.
Running the project locally assumes that java
and MongoDB
are installed locally.
After application is started open the following into any browser http://localhost:8080
Application also provides REST
API for retrieving all users or user by userID
.
To retrieve all users:
http://localhost:8080/api/users
To retrieve user by ID:
http://localhost:8080/api/users/user_id
user_id
should be replaced by real ID
from the main page.
On how to run and build application using Docker
please read this README file or this tutorial:
How to run Spring Boot and MongoDB in Docker container