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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • MongoDB With Spring Boot: A Simple CRUD
  • Translating OData Queries to MongoDB in Java With Jamolingo
  • How to Identify the Underlying Causes of Connection Timeout Errors for MongoDB With Java

Trending

  • Giving AI Agents Safe Access to Legacy Systems: Introducing legacy2mcp
  • Beyond the Endpoint: How Agent Cards Enable Discovery, Interoperability, and Trust
  • Google Cloud KMS vs. AWS KMS: Java Encryption Guide
  • GenAI Security Gateway: An End-to-End Architecture

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
DZone Core CORE ·
Oct. 30, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
11.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Translating OData Queries to MongoDB in Java With Jamolingo
  • How to Identify the Underlying Causes of Connection Timeout Errors for MongoDB With Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook