In the tutorial, JavaSampleApproach will show you way to build a SpringBoot MongoDB RestfulApi.
Related Articles:
– Spring MongoOperations to access MongoDB
– How to use SpringData MongoRepository to interact with MongDB
– How to access MySQL database with Spring Data Rest application
– How to use HAL Browser with Spring Data Rest | SpringBoot + MySQL
– Angular 4 + Spring Boot + MongoDB CRUD example
– Spring Boot + Angular 6 example | Spring Data + REST + MongoDb CRUD example
Contents
I. Technologies
– Java 1.8
– Maven 3.3.9
– Spring Boot: 1.4.3.RELEASE
– Spring Tool Suite – Version 3.8.1.RELEASE
– MongoDB v3.4.1
II. Overview
1. Project Structure
2. Step to do
– Create SpringBoot project
– Create model class
– Create MongoDB Repository
– Run & Check Result
III. Practice
1. Create SpringBoot project
Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input needed project’s info. Press Next, then Finish, a SpringBoot project will be created succesfully.
Open pom.xml file, add Spring Data Rest & Spring Data Mongo dependencies:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> |
2. Create model class
Create a Customer class for simple model:
package com.javasampleapproach.mongodbrestapi.model; import java.io.Serializable; import java.util.concurrent.atomic.AtomicLong; import org.springframework.data.annotation.Id; import org.springframework.data.annotation.PersistenceConstructor; public class Customer implements Serializable { private static final long serialVersionUID = 1L; @Id private Long id; private String firstName; private String lastName; private static AtomicLong COUNTER = new AtomicLong(0L); @PersistenceConstructor public Customer() { this.id = COUNTER.incrementAndGet(); } @Override public String toString() { return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName); } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } |
3. Create a MongoDb repository
Create a MongoDb Repository by extends interface MongoRepository:
package com.javasampleapproach.mongodbrestapi.repository; import java.util.List; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.repository.query.Param; import org.springframework.data.rest.core.annotation.RepositoryRestResource; import com.javasampleapproach.mongodbrestapi.model.Customer; @RepositoryRestResource(collectionResourceRel = "customer", path = "customer") public interface CustomerRepository extends MongoRepository<Customer, String> { List<Customer> findByLastName(@Param("name") String name); } |
In above code, we define a customize method for finding a list customers by lastname.
Open application.properties to configure MongoDB:
spring.data.mongodb.database=restapi spring.data.mongodb.port=27017 |
4. Run & Check Result
Run MongoDB Server. Then run Spring Boot project.
GET request:
localhost:8080
localhost:8080/customer
POST request:
GET all Customers:
– All search query:
– Find customers by LastName:
MongoClient
IV. Source code
Last updated on February 6, 2020.
I had 404 error in the rest client . I use Insomnia, both get and Post method gave 404 INVALID.
Could you please help.
Do you set: “Content-Type”:”application/json” for header?
If not, please try it?
Hi, How to get combination fetch data?
For example we have below records
{ “_id” : ObjectId(“5a704e7643b378971016e914”), “_class” : “Misc_profile_info”, “acctId” : “489704”, “contactId” : “507700”, “keyName” : “favorites”, “valueData” : “KBCContest”, “modifyDate” : ISODate(“2018-01-30T10:52:38.707Z”) }
{ “_id” : ObjectId(“5a704e7643b378971016e915”), “_class” : “Misc_profile_info”, “acctId” : “489704”, “contactId” : “507700”, “keyName” : “Games”, “valueData” : “{\”high-score\”:23000}”, “modifyDate” : ISODate(“2018-01-30T10:52:38.721Z”) }
{ “_id” : ObjectId(“5a704e7643b378971016e916”), “_class” : “Misc_profile_info”, “acctId” : “489704”, “contactId” : “507700”, “keyName” : “Movie01”, “valueData” : “Independenceday”, “modifyDate” : ISODate(“2018-01-30T10:52:38.732Z”) }
{ “_id” : ObjectId(“5a704eab43b378971016e917”), “_class” : “Misc_profile_info”, “acctId” : “489704”, “contactId” : “507700”, “keyName” : “Games”, “valueData” : “{\”high-score\”:23000}”, “modifyDate” : ISODate(“2018-01-30T10:53:31.656Z”) }
{ “_id” : ObjectId(“5a704eab43b378971016e918”), “_class” : “Misc_profile_info”, “acctId” : “489704”, “contactId” : “507700”, “keyName” : “Movie”, “valueData” : “Independenceday”, “modifyDate” : ISODate(“2018-01-30T10:53:31.703Z”) }
But we need exact combination record (“acctId” : “489704”, “contactId” : “507700”, “keyName” : “Movie”). How to get that record? Please suggests.
Hi VamsiKrishna G,
You can use Spring JPA Projection to modify the query view.
More details at: How to query alter domain models by Spring JPA Projection – SpringBoot + MySQL database
Regards,
JSA