MongoDB Blog Post: REST APIs with Java, Spring Boot and MongoDB
The code in this repository is discussed in this blog post in the MongoDB Developer Center.
spring.data.mongodb.uri
in the application.properties
file or set an environment variable: export MONGODB_URI=mongodb://localhost:27017
.mvn spring-boot:run
.mvn clean test
.mvn clean integration-test
.mvn clean package
.java -jar target/java-spring-boot-mongodb-starter-1.0.0.jar
but I would use a real tomcat in production.spring.threads.virtual.enabled=true
in the application.properties
springdoc-openapi-starter-webmvc-ui
library dependency is in the pom.xml
.This project showcases several features of MongoDB:
MongoDBPersonRepository.saveAll()
.MongoDBPersonRepository.getAverageAge()
.MongoDBPersonRepository.java
.ConfigurationSpring.java
.Person.java
.And some other cool stuff:
curl -X 'POST' \
'http://localhost:8080/api/person' \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d '{
"firstName": "Maxime",
"lastName": "Beugnet",
"age": 35,
"address": {
"number": 123,
"street": "avenue des Champs-Elysées",
"postcode": "75000",
"city": "Paris",
"country": "France"
},
"insurance": true,
"cars": [
{
"brand": "Peugeot",
"model": "3008",
"maxSpeedKmH": 280
}
]
}'
curl -X 'GET' 'http://localhost:8080/api/persons' -H 'accept: */*'
Maxime Beugnet