How to use Angular HttpClient to POST, PUT, DELETE data on SpringBoot Rest APIs – Angular 4

With previous posts, we had done 2 important things: fetching data from remote server by Angular HttpClient, and navigate among views by Angular Routing.

In this tutorial, we’ll go to next step – work with Rest APIs: How to use Angular HttpClient to POST, PUT & DELETE data on SpringBoot RestAPI Services.

Related Articles:
How to work with Angular Routing – Spring Boot + Angular 4
How to use Angular Http Client to fetch Data from SpringBoot RestAPI – Angular 4
Angular 4 + Spring JPA + PostgreSQL example | Angular 4 Http Client – Spring Boot RestApi Server
Angular 4 + Spring JPA + MySQL example | Angular 4 Http Client – Spring Boot RestApi Server


I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: RELEASE
– Angular 4

II. Overview

For POST, PUT, DELETE data, We use Angular HTTP Client methods:

– post(url: string, body: any, options?: RequestOptionsArgs): Observable
– put(url: string, body: any, options?: RequestOptionsArgs): Observable
– delete(url: string, options?: RequestOptionsArgs): Observable

Detail implementation:

angular-http-service-architecture

On SpringBoot Server side, we had 3 RequestMappings: @PostMapping, @PutMapping, @DeleteMapping

About Angular Client‘ views, We design a new Add Customer Form and modify Customer Detail‘s view in the previous post as below:

angular http client post put deletet - add customer form

angular http client post put deletet - edit customer form

III. Practice

In the tutorial, we will re-use the sourcecode that we had done with the previous post. So you should check out it for more details:
How to work with Angular Routing – Spring Boot + Angular 4

Step to do:

With Angular Client:
– Implement new CreateCustomerComponent
– Re-Implement CustomerDetailComponent
– Re-Implement App Routing Module
– Re-Implement AppComponent
– Add new functions for DataService: CREATE – UPDATE – DELETE

With SpringBoot Service:
– Implement new Rest APIs: POST – UPDATE – DELETE

Deployment:
– Integrate Angular App and SpringBoot Server
– Run & Check results.

1. Implement new CreateCustomerComponent

Create new CreateCustomerComponent, see the new project’s structure:

angular http client post put deletet - new project structure

CreateCustomerComponent has 3 main functions:
onSubmit() & goBack() map with 2 buttons Submit & Back

angular http client post put deletet - back - submit button

newCustomer() maps with button Add to continuously create a new customer:

angular http client post put deletet - add new customer

Detail Sourcecode:

About CreateCustomerComponent‘s view, We use submitted variable to control the hidden parts:

[(ngModel)] is used for binding data customer: Customer between views and controllers of Angular Application.

2. Re-Implement CustomerDetailComponent

For CustomerDetailComponent, We add 2 new functions: onSubmit() and delete() for handling 2 buttons: Update and Delete

Details Sourcecode:

angular http client post put deletet - update - delete button

About CustomerDetailComponent‘s view, We use form tag to handle Customer’s form submittion and
use submitted variable to control the hidden parts:

3. Re-Implement App Routing Module

Add new path add for CreateCustomerComponent:

4. Re-Implement AppComponent

Modify AppComponent‘s view:

5. Add new functions for DataService: CREATE – UPDATE – DELETE

For CREATE, MODIFY, DELETE customers, in DataService, we implement 3 functions:
– create(customer: Customer): Promise
– update(customer: Customer): Promise
– delete(id: number): Promise

3 Angular HTTPClient‘s functions are used for implementation:

– post(url: string, body: any, options?: RequestOptionsArgs): Observable;
– put(url: string, body: any, options?: RequestOptionsArgs): Observable;
– delete(url: string, options?: RequestOptionsArgs): Observable;

Details sourcecode:

6. Implement new Rest APIs: POST – UPDATE – DELETE

Implement 3 Rest APIs:

>>> Related article: Spring Framework 4.3 New Feature RequestMapping: @GetMapping, @PostMapping, @PutMapping, @DeleteMapping

7. Integrate Angular App and SpringBoot Server

Angular4Client and SpringBoot server work independently on ports 8080 and 4200.
Goal of below integration: the client at 4200 will proxy any /api requests to the server.

Step to do:

– Edit package.json file for “start” script:

If you want to deploy Angular Application on SpringBoot‘s jar file, you can do following:

– Build the angular4client with command ng build --env=prod
– In pom.xml, use Maven plugin to copy all files from dist folder to /src/main/resources/static folder of SpringBoot server project.

>>> Related article: How to integrate Angular 4 with SpringBoot Web App and SpringToolSuite

8. Run & Check results

Build and Run the SpringBoot project with commandlines: mvn clean install and mvn spring-boot:run
Run the Angular App with command: npm start
Make a request: http://localhost:4200/, results:

–> Response’s data

response data - customer list

-> Customer List:

angular http client post put deletet - customer list

Press Add button. Then input new customer’s info:

add new customer

Press Submit button,

customer post - request data

customer post successfully

Press Back button,

result after add John

Then click to select John, edit firstname from ‘John’ to ‘Richard’. Press Update, then press Back button,

richard update data

edit customer

Now click to select Peter, the Application will navigate to Peter details view:

peter details

Press Delete button,

delete peter data

customer list after delete peter

IV. Sourcecode

AngularClientPostPutDelete
SpringBootAngularIntegration


Related Posts


3 thoughts on “How to use Angular HttpClient to POST, PUT, DELETE data on SpringBoot Rest APIs – Angular 4”

  1. I am fairly new to angular 4. How do i work with the zip file you provided for angular code and make it working on my machine

Got Something To Say:

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

*