Skip to content

lydtechconsulting/springboot-mongodb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot Demo With MongoDB

Spring Boot application demonstrating using MongoDB as the datatabase for reading and writing records.

Running The Demo

Build Spring Boot application with Java 17:

mvn clean install

Start Docker containers:

docker-compose up -d

Start Spring Boot application:

java -jar target/springboot-mongodb-1.0.0.jar

In a terminal window use curl to submit a POST REST request to the application to create an item:

curl -i -X POST localhost:9001/v1/items -H "Content-Type: application/json" -d '{"name": "test-item"}'

A response should be returned with the 201 CREATED status code and the new item id in the Location header:

HTTP/1.1 201 
Location: 653d06f08faa89580090466e

The Spring Boot application should log the successful item persistence:

Item created with id: 653d06f08faa89580090466e

Get the item that has been created using curl:

curl -i -X GET localhost:9001/v1/items/653d06f08faa89580090466e

A response should be returned with the 200 SUCCESS status code and the item in the response body:

HTTP/1.1 200 
Content-Type: application/json

{"id":"653d06f08faa89580090466e","name":"test-item"}

In a terminal window use curl to submit a PUT REST request to the application to update the item:

curl -i -X PUT localhost:9001/v1/items/653d06f08faa89580090466e -H "Content-Type: application/json" -d '{"name": "test-item-update"}'

A response should be returned with the 204 NO CONTENT status code:

HTTP/1.1 204 

The Spring Boot application should log the successful update of the item:

Item updated with id: 653d06f08faa89580090466e - name: test-item-update

Delete the item using curl:

curl -i -X DELETE localhost:9001/v1/items/653d06f08faa89580090466e

The Spring Boot application should log the successful deletion of the item:

Deleted item with id: 653d06f08faa89580090466e

Stop containers:

docker-compose down

Component Tests

The test demonstrates the application reading and writing records to MongoDB. They use a dockerised dockerised MongoDB database and a dockerised instance of the application under test.

For more on the component tests see: https://github.com/lydtechconsulting/component-test-framework

Build Spring Boot application jar:

mvn clean install

Build Docker container:

docker build -t ct/springboot-mongodb:latest .

Run tests:

mvn test -Pcomponent

Run tests leaving containers up:

mvn test -Pcomponent -Dcontainers.stayup

Manual clean up (if left containers up):

docker rm -f $(docker ps -aq)

Docker Clean Up

Manual clean up (if left containers up):

docker rm -f $(docker ps -aq)

Further docker clean up if network/other issues:

docker system prune
docker volume prune