In this tutorial, we will create a very simple “single page application” using Angular 10 as the front end and Spring boot as the backend.
Angular is a platform and framework for building single-page client applications using HTML and TypeScript. Angular is written in TypeScript.
Spring boot is popular to develop RESTFul web services and microservices.
Learn spring boot at https://www.javaguides.net/p/spring-boot-tutorial.html
Prerequisites
- Basic familiarity with HTML & CSS
- Basic knowledge of JavaScript and programming
- Spring Boot Basics
- Angular basics
- Node.js and npm installed globally
What we will build?
- sprintboot-backend (server) – To develop REST API
- angular-frontend (client) – Consume REST API
Video
Develop Spring Boot Backend Application
Step1: Create a Spring Boot Application
>> Create Spring Boot Project in Spring Tool Suite [STS]
Step 2: Add Maven dependencies
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>net.javaguides</groupId>
<artifactId>springboot-backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-backend</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Step 3: Create JPA Entity - User.java
package net.javaguides.springboot.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
private String email;
public User() {
}
public User(String firstName, String lastName, String email) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Step 4: Create Spring Data JPA Repository - UserRepository.java
package net.javaguides.springboot.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import net.javaguides.springboot.model.User;
@Repository
public interface UserRepository extends JpaRepository<User, Long>{
}
Step 5: Spring Controller with REST API - /api/users
package net.javaguides.springboot.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import net.javaguides.springboot.model.User;
import net.javaguides.springboot.repository.UserRepository;
@CrossOrigin(origins = "http://localhost:3000")
@RestController
@RequestMapping("api/")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("users")
public List < User > getUsers() {
return this.userRepository.findAll();
}
}
@CrossOrigin(origins = "http://localhost:3000")
Step 6: Run Spring Boot Application and Test Rest API
package net.javaguides.springboot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import net.javaguides.springboot.model.User;
import net.javaguides.springboot.repository.UserRepository;
@SpringBootApplication
public class SpringbootBackendApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SpringbootBackendApplication.class, args);
}
@Autowired
private UserRepository userRepository;
@Override
public void run(String...args) throws Exception {
this.userRepository.save(new User("Ramesh", "Fadatare", "ramesh@gmail.com"));
this.userRepository.save(new User("Tom", "Cruise", "tom@gmail.com"));
this.userRepository.save(new User("Tony", "Stark", "tony@gmail.com"));
}
}
[{"id":1,"firstName":"Ramesh","lastName":"Fadatare","email":"ramesh@gmail.com"},{"id":2,"firstName":"Tom","lastName":"Cruise","email":"tom@gmail.com"},{"id":3,"firstName":"Tony","lastName":"Stark","email":"tony@gmail.com"}]
Build React JS Frontend Application
C:\Angular>node -v
v10.15.3
C:\Angular>npm -v
6.9.0
Install the latest version of Angular CLI 10
npm install -g @angular/cli
Create an Angular 10 App using Angular CLI 10
ng new angular-frontend
Create Component using Angular CLI
– ng g c user
Create a User Model (TypeScript)
export class User { id: number; firstName: string; lastName: string; email: string; }
UserService
- ng g s user
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; import { User } from './user'; @Injectable({ providedIn: 'root' }) export class UserService { private baseUrl = "http://localhost:8080/api/users"; constructor(private http: HttpClient) { } getUsers(): Observable<User[]>{ return this.http.get<User[]>(`${this.baseUrl}`); } }
User Component
import { Component, OnInit } from '@angular/core'; import {UserService} from '../user.service' import { User } from '../user'; @Component({ selector: 'app-user', templateUrl: './user.component.html', styleUrls: ['./user.component.css'] }) export class UserComponent implements OnInit { users: User[]; constructor(private userService: UserService) { } ngOnInit(): void { this.userService.getUsers().subscribe((data: User[]) => { console.log(data); this.users = data; }); } }
UserComponent Template
<div class ="container"> <div class = "card"> <h3 class="text-center"> Users List</h3> <div class = "card-body"> <table class="table table-striped"> <thead> <th>Id</th> <th> First Name</th> <th> Last Name</th> <th> Email </th> </thead> <tbody> <tr *ngFor = "let user of users"> <td> {{ user.id}}</td> <td> {{ user.firstName}}</td> <td> {{ user.lastName}}</td> <td> {{ user.email}}</td> </tr> </tbody> </table> </div> </div> </div>
The Angular Application's Entry Point
If we look inside the angular-frontend folder, we'll see that Angular CLI has effectively created an entire project for us.
Angular's application files use TypeScript, a typed superset of JavaScript that compiles to plain JavaScript. However, the entry point of any Angular application is a plain old index.html file.
Let's edit this file, as follows:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>AngularFrontend</title> <base href="/"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" type="image/x-icon" href="favicon.ico"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> </head> <body> <app-root></app-root> </body> </html>
Note that we have added bootstrap 4 and <app-root> is the root selector that Angular uses for rendering the application's root component.
The app.component.ts Root Component
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'angular-frontend'; }
AppModule
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { HttpClientModule } from '@angular/common/http'; import { AppComponent } from './app.component'; import { UserComponent } from './user/user.component'; @NgModule({ declarations: [ AppComponent, UserComponent ], imports: [ BrowserModule, HttpClientModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
Main (Bootstrap) File
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
Running Angular 10 Client Application
ng serve
ng serve --port 4201
Demo
Further Readings
- Spring Boot + Angular 10 CRUD Example Tutorial
- Spring Boot + Angular 9 CRUD Example Tutorial
- Spring Boot + Angular 8 CRUD Example Tutorial
- Spring Boot + Angular 6 CRUD Example // Popular
- Spring Boot 2 + Angular 7 CRUD Example Tutorial // Popular
- Angular 8 + Spring Boot Example Tutorial // LATEST
- Spring Boot + Angular 8 + WebSocket Example Tutorial // LATEST
Comments
Post a Comment
Leave Comment