Angular 7 + Spring Boot Application: Hello World Example

DZone 's Guide to

Angular 7 + Spring Boot Application: Hello World Example

In this tutorial, we create a full-stack app where we expose an endpoint using Spring Boot and consume this endpoint using Angular 7 and display the data.

· Web Dev Zone ·
Free Resource

In this tutorial, we will create a full-stack application where we expose an endpoint using Spring Boot and consume this endpoint using Angular 7 and display the data. In the next tutorial, we will be further enhancing this application and performing CRUD operations.

Previously, we have seen what a PCF is and how to deploy an application to PCF. I have deployed this application we are developing to PCF. The demo application is as follows-

Angular 7 + Spring Boot Demo

What Is a Full-Stack Application? 

In a full-stack application, we expose the backend point to get the data. This data can then be used by any application or device as per the developer's need. In the future, even if another front-end device is to be used, there will not be much change and the new device will need to consume these endpoints. 

Full Stack Application Example

The project architecture we will be developed as follows:

Angular 7 and Spring Boot Application

This tutorial is explained in the below Youtube Video -


Spring Boot Application

We will be creating a simple Spring Boot Application to expose a REST endpoint to return a list of employees. In a previous tutorial, we had seen how to fetch the data using Spring Boot JDBC. However, for this tutorial, we will be mocking the list of employees to be returned. Maven Project will be as follows:Spring Boot ApplicationThe Maven will be as follows:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">


<description>Demo project for Spring Boot</description>

<relativePath /> <!-- lookup parent from repository -->






Create the SpringBootHelloWorldApplication.java file as shown below:

package com.javainuse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringBootHelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootHelloWorldApplication.class, args);

Create the Employee model class as follows

package com.javainuse.model;

public class Employee {
private String empId;
private String name;
private String designation;
private double salary;

public Employee() {

public String getName() {
return name;

public void setName(String name) {
this.name = name;

public String getDesignation() {
return designation;

public void setDesignation(String designation) {
this.designation = designation;

public double getSalary() {
return salary;

public void setSalary(double salary) {
this.salary = salary;

public String getEmpId() {
return empId;

public void setEmpId(String empId) {
this.empId = empId;

public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((designation == null) ? 0 : designation.hashCode());
result = prime * result + ((empId == null) ? 0 : empId.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(salary);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;

public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (designation == null) {
if (other.designation != null)
return false;
} else if (!designation.equals(other.designation))
return false;
if (empId == null) {
if (other.empId != null)
return false;
} else if (!empId.equals(other.empId))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary))
return false;
return true;

@RequestMapping maps the /employee request to return a list of employees. Also, here we are using the Cross-Origin annotation to specify that calls will be made to this controller from different domains. In our case, we have specified that a call can be made from localhost:4200.

package com.javainuse.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.javainuse.model.Employee;

@CrossOrigin(origins = "http://localhost:4200")
public class TestController {

private List<Employee> employees = createList();

@RequestMapping(value = "/employees", method = RequestMethod.GET, produces = "application/json")
public List<Employee> firstPage() {
return employees;

private static List<Employee> createList() {
List<Employee> tempEmployees = new ArrayList<>();
Employee emp1 = new Employee();

Employee emp2 = new Employee();
return tempEmployees;

Compile and run SpringBootHelloWorldApplication.java as a Java application. Go to localhost:8080/employees.Spring Boot Application Output

Angular 7 Development

Installing Angular CLI

  • Install Node.js by downloading the installable from Install Node.js.
  • Install Angular CLI using the following command. It wll get us the latest version of Angular CKI.
    npm install -g @angular/cli

    angular cli 7 install
  • We can check the Angular LI version, like so: 
    ng version

    angular cli 7 version
  • Next, we will create a new Angular project using the Angular CLI as follows:
    ng new employee-management

    angular cli 7 new project
  • To get the Angular CLI project started use the following command. We must go inside the employee-management folder and then use it.
  • ng serve

    angular cli 7 start

    Go to localhost:4200 angular 7 hello worldI will be using the Microsoft Visual Studio Code IDE for Angular. Let's import the project we developed earlier in Microsoft Visual Studio Code IDE. Our final Angular project's file structure will be as follows: angular 7 application


    TypeScript is a superset of JavaScript. It is a strongly typed language. So, unlike with JavaScript, we know if some syntax is wrong while typing it and rather than having to wait until runtime. In Angular, it is compiled to JavaScript while rendering the application in the browser.


    angular components and servicesIn Angular, we break complex code into reusable parts called components. A major part of development with Angular 7 is done in the components. Components are basically classes that interact with the .html file of the component, which gets displayed on the browser.


    In Angular, we might have scenarios where some code needs to be reused in multiple components. For example, a data connection that fetches data from a database might be needed in multiple components. This is achieved using services.

    Create the Employee Component

    We will be creating an employee component which will fetch data from Spring boot and display it. Lets begin with the employee component. Open your terminal and use the following command:

    ng generate component employee

    angular generate componentFor this, Angular will have created the following four files:

    • employee.component.ts
    • employee.component.spec.ts
    • employee.component.html
    • employee.component.css

    Next, in the app-routing.module.ts file, we will be defining the URL for accessing this component.

    If we got o localhost:4200, we can see the following output employee component works

    Create HttpClient Service

    Next, we will be creating an HTTPClient Service. This service will have the httpClient and will be responsible for calling HTTP GET requests to the backend Spring Boot application. In Angular, a service is written for any cross-cutting concerns and may be used by more than one components

    ng generate service service/httpClient

    angular generate serviceThe following service files are created:

    • http-client.service.ts
    • http-client.service.spec.ts

    We will be modifying the http-client.service.ts file. In the constructor define the HTTPClient instance we will be using to make a call to the Spring Boot application. Here we will be using the Angular HTTPClient for calling the Spring Boot API to fetch the employee data. Also, we will be creating a method which makes calls to the Spring Boot application using the defined httpClient. Also, we need to add the HTTPClientModule to the app.module.ts

    Insert HttpClient Service in Employee Component

    Next, using the constructor dependency injection, we will be providing EmployeeComponen as an instance of HttpClientService. Using this service we make a call to the Spring Boot application to get a list of employees.

    import { Component, OnInit } from '@angular/core';
    import { HttpClientService } from '../service/http-client.service';
      selector: 'app-employee',
      templateUrl: './employee.component.html',
      styleUrls: ['./employee.component.css']
    export class EmployeeComponent implements OnInit {
        private httpClientService:HttpClientService
      ) { }
      ngOnInit() {
         response =>this.handleSuccessfulResponse(response),

    In the employee.component.html file, we iterate over the list of employees we got in the employee.component.ts file.

    <table border="1">
          <tr *ngFor="let employee of employees">

    Go to localhost:4200

    angular 7 output

    And we're done!

    If you enjoyed this article and want to learn more about Angular, check out our compendium of tutorials and articles from JS to 8.

    angular 7, angular tutorial, hello world tutorial, spring boot, web dev

    Published at DZone with permission of Azlan Shaikh . See the original article here.

    Opinions expressed by DZone contributors are their own.

    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}