{"id":20900418,"url":"https://github.com/rahul-ghadge/spring-mongo-docker-compose-crud","last_synced_at":"2025-05-13T01:32:22.252Z","repository":{"id":102526829,"uuid":"301103707","full_name":"rahul-ghadge/spring-mongo-docker-compose-crud","owner":"rahul-ghadge","description":"Dockerized spring boot Mongodb CRUD application using docker-compose for superheroes API. Deploying multiple containers which are depends on each other(Spring App and MongoDB) using single docker-compose command.","archived":false,"fork":false,"pushed_at":"2020-10-04T10:50:20.000Z","size":14,"stargazers_count":2,"open_issues_count":0,"forks_count":6,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-01T15:09:58.158Z","etag":null,"topics":["docker","docker-compose","mongodb","mongotemplate","spring-boot"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rahul-ghadge.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-04T10:45:07.000Z","updated_at":"2022-06-22T09:20:22.000Z","dependencies_parsed_at":"2023-07-09T07:31:13.335Z","dependency_job_id":null,"html_url":"https://github.com/rahul-ghadge/spring-mongo-docker-compose-crud","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rahul-ghadge%2Fspring-mongo-docker-compose-crud","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rahul-ghadge%2Fspring-mongo-docker-compose-crud/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rahul-ghadge%2Fspring-mongo-docker-compose-crud/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rahul-ghadge%2Fspring-mongo-docker-compose-crud/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rahul-ghadge","download_url":"https://codeload.github.com/rahul-ghadge/spring-mongo-docker-compose-crud/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253854069,"owners_count":21974221,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","docker-compose","mongodb","mongotemplate","spring-boot"],"created_at":"2024-11-18T11:19:29.282Z","updated_at":"2025-05-13T01:32:22.242Z","avatar_url":"https://github.com/rahul-ghadge.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# spring-mongo-docker-compose-crud\n\nDocker Compose runs multi-container Docker application. It lets us to run multiple docker container with single docker-compose command by reading configuration file.\nHere we are using multiple containers which are depends on each other(*Mongo container with REST APIs using Spring boot application custom container*).  \nThis project explains dockerized CRUD (**C**reate, **R**ead, **U**pdate, **D**elete) operations with MongoTemplate and MongoRepository using spring boot and mongo DB.\nIn this app we are using Spring Data JPA for built-in methods to do CRUD operations and Mongo queries using MongoTemplate.     \n`@EnableMongoRepositories` annotation is used on main class to Enable Mongo related configuration, which will read properties from `application.yml` file.\n\n\n\n## Prerequisites \n- Java\n- [Spring Boot](https://spring.io/projects/spring-boot)\n- [Maven](https://maven.apache.org/guides/index.html)\n- [Mongo DB](https://docs.mongodb.com/guides/)\n- [Lombok](https://objectcomputing.com/resources/publications/sett/january-2010-reducing-boilerplate-code-with-project-lombok)\n- [Docker Compose](https://docs.docker.com/compose/)\n\n\n## Tools\n- Eclipse or IntelliJ IDEA (or any preferred IDE) with embedded Gradle\n- Maven (version \u003e= 3.6.0)\n- Postman (or any RESTful API testing tool)\n\n\n\u003cbr/\u003e\n\n\n###  Build application\n**cd /absolute-path-to-directory/spring-mongo-docker-compose-crud**  \nand try below command in terminal\n\u003e **```mvn clean install```** it will build application and create **jar** file under target directory \n\n\n### Docker-compose installation\nTo download and install `docker-compose` on Ubuntu visit [this link](https://docs.docker.com/compose/install) and check docker-compose version by below command  \n\u003e docker-compose --version\n\n\nRun below docker-compose command to download and up/deploy containers  \n\u003e docker-compose up --force-recreate  \n\n\nList the containers and check the status with this command  \n\u003e docker-compose ps\n\n\nTo view the container logs, run this command  \n\u003e docker-compose logs\n\n\nTo connect mongo CLI via terminal run below  \n\u003e docker exec -it mongo bash\n\n\n\n||\n|  ---------    |\n| **_Note_** : In `SpringBootMongoDBApplication.java` class we have autowired both SuperHero and Employee repositories. \u003cbr/\u003eIf there is no record present in DB for any one of that module class (Employee or SuperHero), static data is getting inserted in DB from `HelperUtil.java` class when we are starting the app for the first time.| \n\n\n\n### Code Snippets\n\n1. #### Dockerized multi-container Config\n    Two containers i.e. **mongo** and **smdc-crud-superhero-service** will get deployed by running `docker-command up` command which are depends on each other.  \n    **smdc-crud-superhero-service** container is referring **superhero-service** sub-module to deploy jar file in docker by reading **Dockerfile** placed in same sub-module. \n    \n    ```\n    version: '3.8'\n    \n    services:\n      mongo:\n        image: mongo:4.2.10\n        container_name: mongo\n        restart: always\n        ports:\n          - 27017:27017\n        network_mode: host\n        volumes:\n          - $HOME/mongo:/data/db\n        healthcheck:\n          test: \"exit 0\"\n    \n      superhero-service:\n        container_name: smdc-crud-superhero-service\n        build: superhero-service\n        image: superhero-service\n        depends_on:\n          - mongo\n        network_mode: \"host\"\n        hostname: localhost\n        restart: always\n        ports:\n          - 8088:8088\n        healthcheck:\n          test: \"exit 0\"\n    ```\n\n\n\n2. #### Dockerfile to create Spring boot mongo applications jar file in Docker\n    This configuration will download openjdk-8 image and deploy jar file. Same image will be created by reading below configuration and this image will be\n    passed to docker-compose mentioned above.  \n    ```\n    FROM openjdk:8-jdk-alpine\n    VOLUME /tmp\n    ARG JAR_FILE=target/*.jar\n    COPY ${JAR_FILE} spring-mongo-app.jar\n    ENTRYPOINT [\"java\", \"-Djava.security.egd=file:/dev/./urandom\", \"-jar\", \"/spring-mongo-app.jar\"]\n    ```\n\n3. #### Maven Dependencies\n    Need to add below dependencies to enable Mongo related config in **pom.xml**. Lombok's dependency is to get rid of boiler-plate code.   \n    ```\n    \u003cdependency\u003e\n        \u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\n        \u003cartifactId\u003espring-boot-starter-data-mongodb\u003c/artifactId\u003e\n    \u003c/dependency\u003e\n   \n    \u003cdependency\u003e\n        \u003cgroupId\u003eorg.projectlombok\u003c/groupId\u003e\n        \u003cartifactId\u003elombok\u003c/artifactId\u003e\n        \u003coptional\u003etrue\u003c/optional\u003e\n    \u003c/dependency\u003e\n    ```\n   \n   \n4. #### Properties file\n    Reading Mongo DB related properties from **application.yml** file and configuring Mongo connection factory for mongoDB under **superhero-service** module.  \n\n    **src/main/resources/application.yml**\n     ```\n    server:\n        port: 8088\n    spring:\n        data:\n            mongodb:\n                host: localhost\n                port: 27017\n                database: spring_boot_mongo_app\n        application:\n            name: superhero-service \n     ```\n   \n   \n5. #### Model class\n    Below are the model classes which we will store in MongoDB and perform CRUD operations.  \n    **com.spring.mongo.demo.model.SuperHero.java**  \n    ```\n    @Data\n    @AllArgsConstructor\n    @NoArgsConstructor\n    @Builder\n    @Document(collection = \"super_hero\")\n    public class SuperHero implements Serializable {\n    \n        @Id\n        private String id;\n    \n        private String name;\n        private String superName;\n        private String profession;\n        private int age;\n        private boolean canFly;\n    \n        // Constructor, Getter and Setter\n    }\n    ```\n    **com.spring.mongo.demo.model.Employee.java**  \n    ```\n    @Data\n    @AllArgsConstructor\n    @NoArgsConstructor\n    @Builder\n    public class Employee implements Serializable {\n   \n        @Id\n        private String id;\n       \n        private int empId;\n        private String firstName;\n        private String lastName;\n        private float salary;\n        \n        // Constructor, Getter and Setter\n    }\n   ```\n   \n   \n6. #### CRUD operation for Super Heroes\n\n    In **com.spring.mongo.demo.controller.SuperHeroController.java** class, \n    we have exposed 5 endpoints for basic CRUD operations\n    - GET All Super Heroes\n    - GET by ID\n    - POST to store Super Hero in DB\n    - PUT to update Super Hero\n    - DELETE by ID\n    \n    ```\n    @RestController\n    @RequestMapping(\"/super-hero\")\n    public class SuperHeroController {\n        \n        @GetMapping\n        public ResponseEntity\u003cList\u003c?\u003e\u003e findAll();\n    \n        @GetMapping(\"/{id}\")\n        public ResponseEntity\u003c?\u003e findById(@PathVariable String id);\n    \n        @PostMapping\n        public ResponseEntity\u003c?\u003e save(@RequestBody SuperHero superHero);\n    \n        @PutMapping\n        public ResponseEntity\u003c?\u003e update(@RequestBody SuperHero superHero);\n    \n        @DeleteMapping(\"/{id}\")\n        public ResponseEntity\u003c?\u003e delete(@PathVariable String id);\n    }\n    ```\n   \n    In **com.spring.mongo.demo.repository.SuperHeroRepository.java**, we are extending `MongoRepository\u003cClass, ID\u003e` interface which enables CRUD related methods.\n    ```\n    public interface SuperHeroRepository extends MongoRepository\u003cSuperHero, String\u003e {\n    }\n    ```\n   \n   In **com.spring.mongo.demo.service.impl.SuperHeroServiceImpl.java**, we are autowiring above interface using `@Autowired` annotation and doing CRUD operation.\n\n\n\n7. #### JPA And Query operation for Employee\n    In **com.spring.mongo.demo.controller.EmployeeController.java** class JPA related queries API Endpoints are placed.\n    In **com.spring.mongo.demo.controller.EmployeeQueryController.java** class Mongo queries API Endpoints are placed.\n    \n \n \n    \n### API Endpoints\n\n- #### Super Hero CRUD Operations\n    \u003e **GET Mapping** http://localhost:8088/super-hero  - Get all Super Heroes\n    \n    \u003e **GET Mapping** http://localhost:8088/super-hero/5f380dece02f053eff29b986  - Get Super Hero by ID\n       \n    \u003e **POST Mapping** http://localhost:8088/super-hero  - Add new Super Hero in DB  \n    \n      Request Body  \n      ```\n        {\n            \"name\": \"Tony\",\n            \"superName\": \"Iron Man\",\n            \"profession\": \"Business\",\n            \"age\": 50,\n            \"canFly\": true\n        }\n      ```\n    \n    \u003e **PUT Mapping** http://localhost:8088/super-hero  - Update existing Super Hero for given ID \n                                                       \n      Request Body  \n      ```\n        {\n            \"id\": \"5f380dece02f053eff29b986\"\n            \"name\": \"Tony\",\n            \"superName\": \"Iron Man\",\n            \"profession\": \"Business\",\n            \"age\": 50,\n            \"canFly\": true\n        }\n      ```\n    \n    \u003e **DELETE Mapping** http://localhost:8088/super-hero/5f380dece02f053eff29b986  - Delete Super Hero by ID\n\n- #### Employee Get Operations using JPA\n    \u003e **GET Mapping** http://localhost:8088/employee-jpa  - Get all Employees \n    \n    \u003e **GET Mapping** http://localhost:8088/employee-jpa/5f380dece02f053eff29b986  - Get Employee by ID\n    \n    \u003e **GET Mapping** http://localhost:8088/employee-jpa/firstName/Rahul  - Get All Employees with firstname as Rahul \n    \n    \u003e **GET Mapping** http://localhost:8088/employee-jpa/one-by-firstName/Rahul  - Get **ONE** Employee with firstname as Rahul \n    \n    \u003e **GET Mapping** http://localhost:8088/employee-jpa/firstName-like/Rahul  - Get All Employees which contains Rahul in their firstname \n    \n    \u003e **GET Mapping** http://localhost:8088/employee-jpa/one-by-lastName/Ghadage  - Get **ONE** Employee with lastname as Ghadage \n    \n    \u003e **GET Mapping** http://localhost:8088/employee-jpa/salary-greater-than/10000  - Get All Employees whose salary is grater than 1000 \n    \n    \u003e **POST Mapping** http://localhost:8088/employee-jpa/get-by-condition  - Get All Employees with multiple condition \n                                                           \n    Request Body  \n    ```\n    {\n        \"id\": \"5f380dece02f053eff29b986\"\n        \"empId\": \"1\",\n        \"firstName\": \"Rahul\",\n        \"lastName\": \"Khan\",\n        \"salary\": 5000\n    }\n    ``` \n\n- #### Employee Get Operations using Queries\n    \u003e **GET Mapping** http://localhost:8088/employee-query  - Get all Employees \n    \n    \u003e **GET Mapping** http://localhost:8088/employee-query/firstName/Rahul  - Get All Employees with firstname as Rahul \n    \n    \u003e **GET Mapping** http://localhost:8088/employee-query/one-by-firstName/Rahul  - Get **ONE** Employee with firstname as Rahul \n    \n    \u003e **GET Mapping** http://localhost:8088/employee-query/firstName-like/Rahul  - Get All Employees which contains Rahul in their firstname \n    \n    \u003e **GET Mapping** http://localhost:8088/employee-query/one-by-lastName/Ghadage  - Get **ONE** Employee with lastname as Ghadage \n    \n    \u003e **GET Mapping** http://localhost:8088/employee-query/salary-greater-than/10000  - Get All Employees whose salary is grater than 1000 \n    \n    \u003e **POST Mapping** http://localhost:8088/employee-query/get-by-condition  - Get All Employees with multiple condition \n                                                           \n    Request Body  \n    ```\n    {\n        \"id\": \"5f380dece02f053eff29b986\"\n        \"empId\": \"1\",\n        \"firstName\": \"Rahul\",\n        \"lastName\": \"Khan\",\n        \"salary\": 5000\n    }\n    ``` \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frahul-ghadge%2Fspring-mongo-docker-compose-crud","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frahul-ghadge%2Fspring-mongo-docker-compose-crud","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frahul-ghadge%2Fspring-mongo-docker-compose-crud/lists"}