Spring Boot and Angular Integration
Spring Boot and Angular Integration
In this tutorial, we are going to build a spring boot application and will integrate angular application with it. We will first create an empty spring boot application. Then we will add angular application. Later we will integrate the angular application with the spring boot application in the way so that when spring boot application will build, then the angular application will built along with it.
The name of the application is going to be the-doctors-chamber.
Note we are going to build a non-reactive spring application.
Prerequisites
Before further proceeding, you need to have following tools installed in your system:
Maven
Git
Now lets build the application.
Initial Spring Boot Application
For generating a spring boot application, we need to go to https://start.spring.io and mention the dependencies. We will generate the project with the following properties:
Project: Maven project
Spring Boot: 2.6.7
Project Metadata
Group: org.morshed
Artifact: doctors.chamber
Name: doctors.chamber
Description: Project for online doctor-patient consultation
Package: org.morshed.doctors.chamber
Packaging: Jar
Java: 18
Dependencies
Spring Web
Lombok
Spring Boot DevTools
Click on the GENERATE button to generate the project. The project will be downloaded as a zip file. Extract the project and open the project in your favourite IDE (for me, I am using Intellij Idea Ultimate).
Our pom.xml file contains the following contents.
Let’s run the project to see whether our application is ok or not. Before running the project, check whether the JDK 18 is set as the IDE jdk. If we run the project, we will see that the project started on port 8080.
Upload the project to GitHub
We are going to use GItHub as our version controlling system. I assume you already have some basic knowledge on git, if you don’t have, then it is suggested to watch a basic getting started video on youtube.
Go to your github account, create a new repository named doctors-chamber. Also I am going to make the repository as Public so that it can be accessed by anyone. Then click on the Create repository to create the repository.
We need to run the following git commands in the project directory.
The repository address of the project is:
monjurmorshed793/doctors-chamber (github.com)
Our project is set up and we have uploaded the project in a git repository. Now we will add an angular application.
Generating Angular application
We will now generate an angular application. Before the generation, we need to have NodeJs installed (LTS is preferred). Also we need to have angular cli installed in our system. Before we further proceed, let’s create a branch named angular-integration using the following git command:
We can install angular-cli with the following command:
Now we can generate an angular application. Go to the project folder and run the following command:
If we run
Then our angular project will run in port 4200.
Transferring Angular Folders and File
Our purpose is to build angular project when building our Spring Boot application. In order to do that, we need to transfer some of our files to the root project folter. Also we are going to update our .gitignore file as both angular application and spring boot application have this file.
First we will move angular build related files to the root folder.
Go to web folder (angular project folder) and copy all the files except src, .angular, node_modules (note we need to copy .vscode) and paste the files in the root directory (spring boot project root directory). A warning will be shown whether you want to overwrite the existing .gitignore file or not, you should skip overwriting. Here are the files that we moved from the angular project directory to root directory.
Before we further proceed, copy all the contents of .gitignore from the angular project (web) and paste the contents at the end of spring boot projects’ .gitignore file. You may later remove the duplicate mentioned file or folder names, but we may skip this for now. Just you may remove .vscode of the spring boot project and keep .vscode sections from angular’s .gitignore file. The .gitignore file should look like the following.
Your .gitignore my look different, but most of the contents should match. Let us not bother much on the .gitignore file. Let’s proceed.
You may remove the files and folders that we copied from the angular project folder. You should also remove .angular, node_modules folders from the angular project. So the angular project only now has a single folder named src.
As we have moved the folders from angular project to the root directory, we need to update some configurations.
Before we move further, let us commit our changes to the git repo. We need to run the following command to commit our changes to our branch.
Now let’s update the angular configuration files.
Open the angular.json file and we need to replace all the occurrences of src to web/src.You can easily replace the occurrence using VS Code or Intellij Idea.
Then we need to update the outputPath inside the architect section. Replace dist/web with target/classes/static.
Our angular.json configuration file update is done.
Now we will update tsconfig.app.json file. Open the file and replace all the occurrences of src to web/src. Our tsconfig.app.json file needs only this replacements.
Also we need to replace all the occurrences of src to web/src in tsconfig.spec.json.
That’s all for now. Now we will run our angular application. But before that as we have changed our directory, we need to run npm install in the project root directory first to install necessary libraries.
After that, let’s run the angular application by running ng serve in the root directory (spring boot project directory) and check whether our angular application runs or not. If you followed till now, then you will see the application run is successful.
Now we will configure our pom.xml file in order to build angular application during the project build.
Configuring pom.xml file
For building the angular app during the spring boot project build, we need a plugin named frontend-maven-plugin.
Add the following plugin in the pom.xml file.
Our pom.xml file now looks like this:
Here, in the plugin, we are using nodejs version 14.18.0. During the build, npm install command is executed. Then npm build command is executed from the package.json file.
Let’s check whether our configuration is ok or not. Let’s run the spring boot application by running the command
In the log, we will see that npm commands are being run and when the build is complete and the output logs shows that the project is running in port 8080, then if you go to http://localhost:8080 then you will see our angular app as shown in the figure.
In this blog, we have configured angular and spring boot app. New tutorials will shoon be added.
Comments
Post a Comment