Angular 4 + Spring Boot + MongoDB CRUD example

In this tutorial, JavaSampleApproach shows you Angular 4 Http Client & Spring Boot Server example that uses Spring Data to interact with MongoDB and Angular 4 as a front-end technology to make request and receive response.

Related Posts:
How to use Angular Http Client to fetch Data from SpringBoot RestAPI – Angular 4
How to use Angular HttpClient to POST, PUT, DELETE data on SpringBoot Rest APIs – Angular 4
How to build SpringBoot MongoDb RestfulApi
How to use SpringData MongoRepository to interact with MongoDB

Full Reactive:
Angular 4 + Spring WebFlux + Spring Data Reactive MongoDB example | Full-Reactive Angular 4 Http Client – Spring Boot RestApi Server

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite 3.9.0.RELEASE
– Spring Boot 1.5.8.RELEASE
– Angular 4
– MongoDB 3.4.10

II. Overview

angular-http-service-architecture

1. Spring Boot Server

angular4-springdata-mongodb-server-overview

Spring Data MongoDB – MongoRepository example:
How to use SpringData MongoRepository to interact with MongoDB

2. Angular 4 Client

angular4-springdata-mongodb-httpclient-overview

For more details:
– About Angular 4 Routing:
How to work with Angular Routing – Spring Boot + Angular 4
– About Angular Http Client to GET/POST/DELETE:
+ How to use Angular Http Client to fetch Data from SpringBoot RestAPI – Angular 4
+ How to use Angular HttpClient to POST, PUT, DELETE data on SpringBoot Rest APIs – Angular 4

III. Practice

1. Project Structure

1.1 Spring Boot Server

angular4-springdata-mongodb-server-structure

– Class Customer corresponds to document in customer collection.
CustomerMongoRepository is an interface extends MongoRepository, will be autowired in CustomerController for implementing repository methods and custom finder methods.
CustomerController is a REST Controller which has request mapping methods for RESTful requests such as: getAll, create, update, delete Customers.
– Configuration for Spring Data MongoDB properties in application.properties
– Dependencies for Spring Boot and Spring Data MongoDB in pom.xml

1.2 Angular 4 Client

angular4-springdata-mongodb-client-structure

In this example, we have:
– 3 components: customers-list, customer-details, create-customer.
– 3 modules: FormsModule, HttpClientModule, AppRoutingModule.
customer.ts: class Customer (id, name, age, active).
customer.service.ts: Service for HttpClient methods.

2. How to do

2.1 Spring Boot Server
2.1.1 Dependency

2.1.2 Customer – Data Model

2.1.3 MongoDB Repository

2.1.4 REST Controller

2.1.5 Configuration for Spring Data MongoDB

2.2 Angular 4 Client
2.2.1 AppModule

2.2.2 Model

2.2.3 DataService

2.2.4 Components

– CustomerDetailsComponent:

– CustomersListComponent:

– CreateCustomerComponent:

2.2.5 AppRoutingModule

2.2.6 App Component

3. Run & Check Result

Build and Run Spring Boot project with commandlines: mvn clean install and mvn spring-boot:run.
– Run the Angular App with command: npm start.

– Open browser for url http://localhost:4200/:
Add Customer(Jack,27):
angular4-springdata-mongodb-result-add-customer

Add more customers, check MongoDB collection:
angular4-springdata-mongodb-result-show-customers-cmd

Choose tab Customers:
angular4-springdata-mongodb-result-show-customers

Update active status by click on Active label:
angular4-springdata-mongodb-result-update-customer

Delete a customer (Katherin):
angular4-springdata-mongodb-result-delete-customer

Check MongoDB collection again, Jack‘s active status has changed, and Katherin data has been deleted:
angular4-springdata-mongodb-result-after-customers-cmd

Click Delete All button:
angular4-springdata-mongodb-result-delete-all-customers

IV. Source Code

SpringBootAngular4MongoDB
Angular4MongoDB

By JavaSampleApproach | November 28, 2017.

Last updated on May 1, 2018.


Related Posts


3 thoughts on “Angular 4 + Spring Boot + MongoDB CRUD example”

    1. Hi shashi kumar,

      You can download the source code for checking.
      The application in this tutorial doesn’t have any specific configuration. 🙂

      Regards,
      JSA.

Got Something To Say:

Your email address will not be published. Required fields are marked *

*