Employee Manager App that permits CRUD operations through Angular SPA that consumes a SpringBoot REST API connected to MySQL Database.
This project is adapted and feature-enhanced from Spring Boot Full Stack with Angular App - Full Course 05-Feb-2021 - Amigoscode - 2h40m tutorial. The complete documentation for this project can be found here: Spring Boot Full Stack with Angular Application - Full Course 05-Feb-2021 - Amigoscode.
Contents:
- Employee Manager App with Angular and SpringBoot Rest API
For Employees MySQL Table:
-
http://localhost:8080/api/employees- GET -getAllEmployees() -
http://localhost:8080/api/employees/{id}- GET -getEmployeeById(@PathVariable("id") Long id) -
http://localhost:8080/api/employees/new- POST -addEmployee(@RequestBody Employee employee) -
http://localhost:8080/api/employees/update- PUT -updateEmployee(@RequestBody Employee employee) -
http://localhost:8080/api/employees/{id}- DELETE -deleteEmployee(@PathVariable("id") Long id)
For Salaries MySQL Table:
-
http://localhost:8080/api/salaries- GET -findAllSalaries() -
http://localhost:8080/api/salaries/{id}- GET -findSalaryById() -
http://localhost:8080/api/salaries/employee/{id}- GETfindSalariesByEmployeeId() -
http://localhost:8080/api/salaries/new- POSTaddSalary() -
http://localhost:8080/api/salaries/update- PUTupdateSalary() -
http://localhost:8080/api/salaries/{id}- DELETEdeleteSalaryById()
MySQL Database Diagram:
Main page:
About Page:
Main Page - Add Employee Form:
Main Page - Quick Edit Employee Form:
Main Page - Delete Employee Form:
Employee Page - Editable information:
Employee Page - Edit, Delete & Add Salaries Form:
git clone https://github.com/radualexandrub/SpringBoot-Angular-EmployeeManagerApp SpringBoot_EmployeeManagerApp-
First, start the MySQL Service:
- In Windows, open the Start Menu, search and open "Services", manually find MySQL80 service -> Right click it -> Start
- In Debian, run
sudo systemctl start mysql.service
-
Set up the root (or user) password for MySQL:
- In Windows, you can set the root password during the MySQL installation process
- In Debian, right after installing MySQL (
sudo apt install mysql-server), you can runsudo mysql_secure_installationto run the MySQL Installation script (you can follow along this tutorial from DigitalOcean)
-
In SpringBoot app, go to
src/main/resources/application.propertiesand set thespring.datasource.passwordto match the password set for MySQL -
Finally, create the
employeemanagerdatabase:- You can create this database by opening MySQL Workbench GUI, log in, and run
create database employeemanager;(or you can run this command from MySQL CLI in Windows) - In Debian, run
sudo su, thensudo mysql -u root -pto login and access the MySQL CLI, then runcreate database employeemanager;
- You can create this database by opening MySQL Workbench GUI, log in, and run
-
Check the database with the following commands:
show databases;use employeemanager;show tables;(it should be empty if SpringBoot app was never ran)select * from employee;(it should return error SpringBoot app was never ran)
-
For x64 Windows:
- (Recommended) You can download the JDK (Java Development Toolkit) separately from here: https://www.oracle.com/java/technologies/downloads/#jdk17-windows
- Go to "System Properties" (Can be found on Control Panel > System and Security > System > Advanced System Settings)
- Click on the "Environment variables" button under the "Advanced" tab
- Then, select the "Path" variable in System variables and click on the "Edit" button
- Click on the "New" button and add the path where Java is installed, followed by \bin. By default, Java is installed in
C:\Program Files\Java\jdk-11.0.1(If nothing else was specified when you installed it). In that case, You will have to add a new path with:C:\ProgramFiles\Java\jdk-11.0.1\binThen, click "OK", and save the settings - Restart PC
- Open Command Prompt (cmd.exe) and type
java -versionto see if Java is running on your machine - Then, you can download and install Maven from here: https://maven.apache.org/download.cgi
-
For x64 Debian based systems (Ubuntu, Linux Mint, Zorin OS, KDE Neon, etc):
- Run the following commands based on this tutorial
sudo apt install default-jresudo apt install default-jdkjava -versionjavac -versionsudo update-alternatives --config javasudo update-alternatives --config javac
- Set the
$JAVA_HOMEEnvironment Variablesudo nano /etc/environment- write
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64" cat /etc/environment
- Install Maven:
sudo apt install mavenmvn -v
- Run the following commands based on this tutorial
-
For x64 Windows:
- First, download Node.js - we need it to use NPM (Node Package Manager)
- Make sure you have
C:\Program Files\nodejs\path on yourSystem Properties > Environment Variables > System Variables > Path. Restart PC. - Then install the Angular CLI: To install the Angular CLI globally, open a terminal window and run the following command (You will run this only once):
npm install -g @angular/cli - Within
employeemanager-angularAngular App folder, runnpm install
-
For x64 Debian based systems (Ubuntu, Linux Mint, Zorin OS, KDE Neon, etc):
- Run
curl -s https://deb.nodesource.com/setup_16.x | sudo bash(to get Node v16) - Run
sudo apt install nodejs -y - Install npm
sudo npm install -g npm@8.19.2 - Install Angular
sudo npm install -g @angular/cli - Within
employeemanager-angularAngular App folder, runnpm install
- Run
The complete project (MySQL + SpringBoot BackEnd + Angular FrontEnd) can be started with these steps:
- start the MySQL Server by opening Windows Start Menu, search and open "Services", manually find MySQL80 service -> Right click it -> Start (for Linux, run
sudo systemctl start mysql.service) - start the SpringBoot Back-end Server with
mvn spring-boot:runand test on http://localhost:8080/api/employees - start the Front-end Angular Application with
ng serve --openand open http://localhost:4200/
If you cannot install MySQL, Java, Maven and Node.js/Angular on your machine, you can run this Application with Docker.
π΅ Note: The containers will run this App for a Production environment:
- Front-End (configured in
Angular.Dockerfile) will run the static build with Nginx - Back-End (configured in
Spring.Dockerfile) will run the application's built package - MySQL8 (configured in
docker-compose.yml)
Steps:
- Clone this repo
git clone https://github.com/radualexandrub/SpringBoot-Angular-EmployeeManagerApp employeemanager
cd employeemanager- Build (and run as containers) the database, front-end and back-end images via
docker-compose.ymlfile withdocker compose upcommand
docker compose up
# or -d to supress logs (Detached mode: Run containers in the background)
docker compose up -d-
Open the front-end app (on http://localhost:8081/) and the back-end Spring API (on http://localhost:8080/)
-
Play with the app (you can follow the app's logs with
docker-compose logs -f) -
Stop all Docker containers
docker-compose down
# or use -v to also remove the volumes used for MySQL, also remove other remaining containers
docker-compose down -v --remove-orphans- Remove built Docker images
# View images and their IDs
docker images -a
# Remove images by their IDs
docker rmi <angular_appImageID>
docker rmi <spring_appImageID>
docker rmi <mysql_db_ImageID>- Show Docker disk usage
docker system df- (Optional) Remove all Docker data (including Images, Containers, Local Volumes and Build Cache)
docker system pruneDocker resources I used:
- Learn Docker in 7 Easy Steps - Beginner's Tutorial by Fireship.io - Study Notes by Radu Alexandru B
- Docker Compose: Spring Boot and MySQL example - from BezKoder.com
- How to Dockerize Angular with NGINX and Spring Boot with MySQL using Docker Compose - from javachinna.com
Issues encountered when running Docker containers:
- Dockerfile after build container doesnβt work βCould not find or load main classβ - solved by adding whole path to
main()java function (eg.com.radubulai.employeemanager.EmployeemanagerApplication) - Unable to open JDBC Connection for DDL execution
- Communications link failure , Spring Boot + MySql +Docker + Hibernate
- Could not create connection to database server via Docker - got the Idea of delaying/waiting for MySQL8 container to fully be operational before Spring tries to connect to the database
- Spring Boot + docker-compose + MySQL: Connection refused - adding
healthcheckto MySQL Image indocker-compose.yml - Docker Compose wait for container X before starting Y
- Connection Java - MySQL : Public Key Retrieval is not allowed - adding client option to mysql-connector
allowPublicKeyRetrieval=trueto allow the client to automatically request the public key from the server =>jdbc:mysql://mysql-db:3306/employeemanager?autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
CORS related issues while running Docker containers and using the app:
- API Net 6 :: CORS issues when running inside Docker container
- 'Access-Control-Allow-Origin' with spring boot
- Added
'Access-Control-Allow-Origin' '*'in header in Angular App that is running on Nginx
server {
listen 80;
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin';
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
}Issues encountered when running docker compose up on Debian (Linux):
- Download and install Docker Desktop
.debpackage (that containsdocker compose) for Debian from here or RPM package for Fedora from here - mvnw: Permission denied - fixed by making
mvnwfile executable - runchmod +x mvnwwithin the root project path - Error starting userland proxy: listen tcp4 0.0.0.0:3306: bind: address already in use - fixed by running
sudo netstat -nlpt | grep 3306(installed bysudo apt instal net-tools) which finds MySQL running on port 3306 -> stop local MySQL from running withsudo systemctl stop mysql.service
Issues encountered when running docker compose build on a Windows machine:
=> ERROR [employeemanager_spring-api build 6/9] RUN ./mvnw dependency:go-offline -B 0.3s
------
> [employeemanager_spring-api build 6/9] RUN ./mvnw dependency:go-offline -B:
#26 0.237 /bin/sh: 1: ./mvnw: not found
------
failed to solve: executor failed running [/bin/sh -c ./mvnw dependency:go-offline -B]: exit code: 127
- You can solve this error by having an Ubuntu WSL installed on your Windows machine. In Ubuntu WSL, run
sudo apt-get install dos2unix, followed bysudo apt-get update -y && sudo apt-get upgrade -y - Inside the main project's folder, run
dos2unix mvnw - References:
- As per commit "Update README with Docker Issue encounterd on Windows and mvnw" from Thursday, December 01, 2022, running
git diffresulted in:
git diff
warning: LF will be replaced by CRLF in mvnw.
The file will have its original line endings in your working directory
- The
mvnwfile might need to be changed according to the PC (Linux or Windows) that is running the containers
See steps for running the app with podman-compose up as well as troubleshooting steps here: Migrating from docker compose to podman compose
To be able to debug our application (using breakpoints in IntelliJ) after running the docker-compose up command, we need to perform the following steps:
- In the main project in
application.propertiesfile, adddebug=trueline (if usingapplication.ymlfile, adddebug: trueinstead)
- In
docker-compose.ymlfile:
- Under
spring-apiservice >ports, we need to expose the 5005 debug port:
- "5005:5005" # Expose the debug port 5005 on the container to port 5005 on the host machine- In IntelliJ:
- Go to
Run>Edit Configurations... - Click the "+" button to add a new configuration and select "Remote JVM Debug" from the list
- Set a name for the configuration (e.g. "Remote Debugging").
- Under the "Debugger mode" section, select "Attach to remote JVM"
- Set the "Host" to
localhostand the "Port" to5005(or the port you specified in thedocker-compose.ymlfile) - Copy to clipboard the command under "Command line arguments for remote JVM"
- Click "Apply" and then "OK" to save the configuration
- Again, in
docker-compose.ymlfile:
- Under
spring-apiservice >environment, we need to addJAVA_TOOL_OPTIONSenvironment variable. This configuration enables the JVM to listen on port 5005 for remote debugging connections (Paste the value from "Command line arguments for remote JVM)
JAVA_TOOL_OPTIONS: "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"-
Open a separate terminal (e.g. Powershell), and run
docker-compose upcommand -
In IntelliJ:
- Add breakpoints in your codebase where you want to inspect/debug code. The debugger will stop at those breakpoints during the execution
- Have the
docker-compose.ymlfile opened, then:- Go to upper right corner, select the created "Remote Debugging" configuration, and click on "Debug 'Remote Debugging'" (the bug icon) - or press SHIFT+F9 shortcut (to start the remote debugging session). IntelliJ will now connect to the running Spring Boot application in the Docker container via the debug port
- Open the bottom "Debug" tab
Useful links used:
- Run and debug a Spring Boot application using Docker Compose - IntelliJ Ultimate
- How to Debug Docker Containers in IntelliJ - 12min YouTube video from 13-Aug-2022
As of Wednesday, November 30, 2022
employeemanager/
β£ .m2/
β£ .mvn/
β£ employeemanager-angular/
β β£ .angular/
β β β cache/
β β β 13.2.6/
β β£ Angular.Dockerfile
β β src/
β β£ app/
β β β£ components/
β β β β£ employee-card/
β β β β£ header/
β β β β modals/
β β β services/
β β£ assets/
β β environments/
β£ src/
β β£ main/
β β β£ java/
β β β β com/radubulai/employeemanager/
β β β β£ exception/
β β β β£ model/
β β β β£ repo/
β β β β£ service/
β β β β EmployeemanagerApplication.java
β β β resources/
β β β£ static/
β β β templates/
β β test/
β£ target/
β£ mvnw
β£ mvnw.cmd
β£ pom.xml
β£ Spring.Dockerfile
β docker-compose.yml
Copyright Β© 2022-2024 Radu-Alexandru Bulai. Released under MIT License.
π΄ Please ensure proper credit is given when using this application for training purposes in your company. For any inquiries, kindly email me at radualexandrubulai@gmail.com. Thank you!












