DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • MongoDB With Spring Boot: A Simple CRUD
  • Multi-Tenancy Implementation Using Spring Boot, MongoDB, and Redis
  • How Spring and Hibernate Simplify Web and Database Management

Trending

  • Chat With Your Knowledge Base: A Hands-On Java and LangChain4j Guide
  • Monolith: The Good, The Bad and The Ugly
  • The Future of Java and AI: Coding in 2025
  • How to Create a Successful API Ecosystem
  1. DZone
  2. Data Engineering
  3. Databases
  4. A CRUD With MongoDB and Java in One Line of Code

A CRUD With MongoDB and Java in One Line of Code

In this quick tutorial, I'll show you how to implement a web CRUD (Create, Read, Update, and Delete) using MongoDB, Spring Boot, and Vaadin.

By 
Alejandro Duarte user avatar
Alejandro Duarte
DZone Core CORE ·
Oct. 30, 20 · Tutorial
Likes (15)
Comment
Save
Tweet
Share
11.5K Views

Join the DZone community and get the full member experience.

Join For Free

In this quick tutorial, I'll show you how to implement a web CRUD (Create, Read, Update, and Delete) using MongoDB, Spring Boot (version 2.3.4), and Vaadin (version 14.3.7). I assume you have MongoDB, the JDK version 8 or later, Maven, and a Java IDE installed on your computer.

Here's a video version of this tutorial if you prefer:

Step 1: Create a New Spring Boot Project

Go to start.spring.io and create a new project with the following dependencies:

  • Vaadin - Java framework for building web apps on Web Components
  • Spring Data Mongo DB - Store data in JSON-like documents
  • Lombok (optional) - Java annotation library to reduce boilerplate code

Download and extract the ZIP file. The extracted content is a Maven project that you can import into your favorite IDE.

Add the following repository and dependency to your pom.xml file:

XML
 




x


 
1
<repository>
2
    <id>vaadin-addons</id>
3
    <url>https://maven.vaadin.com/vaadin-addons</url>
4
</repository>
5

          
6
<dependency>
7
    <groupId>org.vaadin.crudui</groupId>
8
    <artifactId>crudui</artifactId>
9
    <version>4.3.1</version>
10
</dependency>
9
            <artifactId>crudui</artifactId>



Step 2: Create a Domain Class

Create a new Student class as follows:

Java
 




xxxxxxxxxx
1
16


 
1
import lombok.*;
2
import org.springframework.data.annotation.Id;
3

          
4
@Data
5
@NoArgsConstructor
6
@AllArgsConstructor
7
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
8
public class Student {
9

          
10
    @Id
11
    private Integer regNumber;
12
    private String firstName;
13
    private String lastName; 
14

          
15
}



Step 3: Create a Repository Interface

Create a new StudentRepository interface as follows:

Java
 




xxxxxxxxxx
1


 
1
import org.springframework.data.mongodb.repository.MongoRepository;
2

          
3
public interface StudentRepository extends MongoRepository<Student, Integer> {
4
}



Step 4: Create a Service Class

Create a new StudentService class as follows:

Java
 




xxxxxxxxxx
1
36


 
1
import org.springframework.stereotype.Repository;
2
import org.vaadin.crudui.crud.CrudListener;
3

          
4
import java.util.Collection;
5

          
6
@Repository
7
public class StudentService implements CrudListener<Student> {
8

          
9
    private final StudentRepository repository;
10

          
11
    public StudentService(StudentRepository repository) {
12
        this.repository = repository;
13
    }
14

          
15
    @Override
16
    public Collection<Student> findAll() {
17
        return repository.findAll();
18
    }
19

          
20
    @Override
21
    public Student add(Student student) {
22
        return repository.insert(student);
23
    }
24

          
25
    @Override
26
    public Student update(Student student) {
27
        return repository.save(student);
28
    }
29

          
30
    @Override
31
    public void delete(Student student) {
32
        repository.delete(student);
33
    }
34

          
35
}



Step 5: Implement a View Class

Create a new class as follows:

Java
 




xxxxxxxxxx
1
15


 
1
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
2
import com.vaadin.flow.router.Route;
3
import org.vaadin.crudui.crud.impl.GridCrud;
4

          
5
@Route("")
6
public class StudentsView extends VerticalLayout {
7

          
8
    public StudentsView(StudentService service) {
9
        GridCrud<Student> crud = new GridCrud<>(Student.class, service);
10
        add(crud);
11
        setSizeFull();
12
    }
13

          
14
}



See line 9. With this one line, you are creating the actual CRUD web UI component.

Step 6: Run the Application

Make sure MongoDB is running and start the application as follows:

Plain Text
 




xxxxxxxxxx
1


 
1
mvn spring-boot:run



Note: The first time you run the application will take more time than subsequent runs.

Go to http://localhost:8080 and try the application:

add name

Next Steps

You can configure the MongoDB connection string in the application.properties file. For example:

Properties files
 




xxxxxxxxxx
1


 
1
spring.data.mongodb.uri=mongodb://localhost/test
2
spring.data.mongodb.username=user
3
spring.data.mongodb.password=password



You can customize the CRUD component. For example:

Java
 




xxxxxxxxxx
1


 
1
crud.getCrudFormFactory().setVisibleProperties("firstName", "lastName");
2
crud.getGrid().setColumns("firstName", "lastName");



There are many more customization options. See the examples at https://vaadin.com/directory/component/crud-ui-add-on.

Java (programming language) MongoDB Spring Framework

Opinions expressed by DZone contributors are their own.

Related

  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • MongoDB With Spring Boot: A Simple CRUD
  • Multi-Tenancy Implementation Using Spring Boot, MongoDB, and Redis
  • How Spring and Hibernate Simplify Web and Database Management

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!