In this tutorial, I will show you step by step to integrate Angular 12 project with Spring Boot Rest API so that Spring Boot and Angular 12 will run in one project. You will also know how to configure Angular 12 SPA Routing to avoid Whitelabel Error Page.
Related Posts:
– Angular 12 + Spring Boot + H2 example
– Angular 12 + Spring Boot + MySQL example
– Angular 12 + Spring Boot + PostgreSQL example
– Angular 12 + Spring Boot + MongoDB example
– Angular 12 + Spring Boot: Pagination example
– Angular 12 + Spring Boot: JWT Authentication example
– Angular 12 + Spring Boot: File upload example
Other versions:
– Integrate Angular 8 with Spring Boot Rest API
– Integrate Angular 10 with Spring Boot Rest API
– Integrate Angular 11 with Spring Boot Rest API
Contents
Angular 12 & Spring Boot application Overview
Assume that we have 2 projects: Angular 12 & Spring Boot:
For example, if we run them separately:
- Spring Boot Server exports Rest Apis at Url:
http://localhost:8080/
- Angular 12 Client runs at url:
http://localhost:8081/
Using Angular 12 to call Spring Rest API:
Otherwise, when deploying Angular 12 production-build with Spring Boot project, we only need to run Spring Boot Project for the fullstack (Angular 12 + Spring Boot) system.
In this example, we access http://localhost:8080/
.
Technology Stack
- Node.js
- Angular 12
- Spring Boot 2
- Spring Tool Suite
- Maven 3.6.1
Setup Angular 12 Client
You can use your own Angular 12 Project, or just download the source code on Github, or follow these steps to create a new one.
Open cmd and use Angular CLI to create a new Angular 12 Project as following command:
ng new AngularClient
? Would you like to add Angular routing? Yes
? Which stylesheet format would you like to use? CSS
We also need to generate some Components and Services:
ng g s services/tutorial
ng g c components/add-tutorial
ng g c components/tutorial-details
ng g c components/tutorials-list
Now you can see that our project directory structure looks like this-
Because I want to keep this tutorial simple and brief, please continue to develop this App with instruction in the post:
Angular 12 CRUD Application example with Web API
Import Angular 12 Project to Spring Tool Suite
Open Spring Tool Suite, right click on Package Explorer and choose Import -> General -> Projects from Folder or Archieve, press Next.
Find the Angular Project that we’ve just created above and press Finish, angular-client is imported like this:
To clean the source code in STS, we need to remove node_modules folder by following the steps:
- Right click on angular-client project, choose Properties, then Resource -> Resource Filter.
- Press Add Filter…, choose Filter Type: Exclude all, Applies to: Files and folders, and check All children (recursive), with ‘File and Folder Atributes’, we specify node_modules:
Press OK, then Apply, the result will be like this:
Setup Spring Boot Server
You can use your own Spring Boot Project, or just download the source code on Github, or follow these steps to create a new one.
Use Spring web tool or your development tool (Spring Tool Suite, Eclipse, Intellij) to create a Spring Boot project.
Then open pom.xml and add these 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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
The instruction can be found in the post:
Spring Boot, Spring Data JPA – Rest CRUD API example
Now we have 2 projects together in Spring Tool Suite:
Let’s continue to the most important part.
Integrate Angular 12 with Spring Boot Rest API
Build Angular 12 App
Currently Angular Client and Spring Boot server work independently on ports 8081
and 8080
.
The first thing we need to do is to build Angular App for production.
There are 2 steps:
– Set the output directory to static folder:
Open angular.json, add the "outputPath": "./static"
option to the build
target so that the production will be stored in static folder under project root directory.
{
...
"projects": {
"Angular12ClientCrud": {
...
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "./static",
"index": "src/index.html",
"main": "src/main.ts",
...
},
"configurations": {
...
}
},
...
}
}},
"defaultProject": "Angular12ClientCrud"
}
– Run command ng build
.
✔ Browser application bundle generation complete.
✔ Copying assets complete.
✔ Index html generation complete.
Initial Chunk Files | Names | Size
main.d5a3d3feaca11180e4a3.js | main | 252.65 kB
polyfills.9bfe7059f6aebac52b86.js | polyfills | 35.95 kB
runtime.d096b1053e110ec9273b.js | runtime | 1.01 kB
styles.31d6cfe0d16ae931b73c.css | styles | 0 bytes
| Initial Total | 289.61 kB
Build at: 2021-06-07T03:05:29.716Z - Hash: de64dc4c3e81e6c52efe - Time: 56612ms
Now you can see new static folder with content as following:
Integrate Angular 12 production into Spring Boot Project
Now we need to copy all files from Angular static folder to src/target/classes/static folder of Spring Boot project.
There are 2 way to do this:
- Manually copy/paste
- Using maven-resources-plugin
Open pom.xml, add following plugin:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/static/</outputDirectory>
<resources>
<resource>
<directory>${basedir}/../angular-client/static</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
In the code above, we specify outputDirectory
for the destination folder, and resource>directory
for the source folder.
Run and Check Result
Build and run the Spring Boot server with commands:
– mvn clean install
– mvn spring-boot:run
Open browser with url: http://localhost:8080/
.
The result:
Spring Boot + Angular 12: Whitelabel Error Page
Oh yeah! Everything looks good.
But wait, let’s try to refresh the page.
What happened?
To handle this error, we’re gonna enable hash(#) in App Routing module.
– Open app-routing.module.ts
– Pass optional parameter useHash
as true
value.
@NgModule({
imports: [RouterModule.forRoot(routes, {useHash: true})],
exports: [RouterModule]
})
Our Url is hashed(#) after port number: http://localhost:8080/#/tutorials
Now you can refresh the page without worrying about Whitelabel Error.
Conclusion
Today we’ve learned how to integrate Spring Boot Project with Angular 11 Application in one Project. We also handle “Whitelabel Error Page” case for Spring Boot + Angular 11 Project.
There are many Spring Boot + Angular 11 examples that you can apply this approach to integrate:
– Angular 12 + Spring Boot + H2 example
– Angular 12 + Spring Boot + MySQL example
– Angular 12 + Spring Boot + PostgreSQL example
– Angular 12 + Spring Boot + MongoDB example
– Angular 12 + Spring Boot: Pagination example
– Angular 12 + Spring Boot: JWT Authentication example
– Angular 12 + Spring Boot: File upload example
Further Reading
Other versions:
– Integrate Angular 8 with Spring Boot Rest API
– Integrate Angular 10 with Spring Boot Rest API
– Integrate Angular 11 with Spring Boot Rest API
I like the helpful practice you provide in your fullstack tutorials. Thanks!