You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Creating a New Application with Spring Boot and Angular
2
+
3
+
Spring Boot works great as a back end for an Angular application but it can be difficult to get the ball rolling. Most Spring users are comfortable with Java and the tools that are used to create and build the backend server. The front end can be written with plain old JavaScript as long as it is relatively simple, and you are willing to search for the rare examples and tutorials in this style. But these days you are much more likely to find documentation and tutorials that use tools like `Typescript`, `node.js`, `npm` and the Angular CLI.
4
+
5
+
This article shows you how to do that and keep your Spring Boot roots. Much of the advice would apply equally well to other front end frameworks (anything that can be built using `npm` or similar). We use Maven, but similar tools are available for Gradle users. The goal is to have a single application that has Spring Boot and Angular, that can be built and developed by anyone who has knowledge of either ecosystem, and does not feel awkward or unidiomatic to either.
6
+
7
+
## Install Npm Locally
8
+
9
+
Installing `npm` is fraught with issues, including but not limited to how to get it working as part of your build automation. We are going to use the excellent [Maven Frontend Plugin](https://github.com/eirslett/frontend-maven-plugin) from Eirik Sletteberg. The first step is to add it to our `pom.xml`:
2
10
3
11
```
4
12
<build>
@@ -31,26 +39,35 @@
31
39
</build>
32
40
```
33
41
34
-
then
42
+
and then
35
43
36
44
```
37
45
$ mvn generate-resources
38
46
$ ls node*
39
47
```
40
48
49
+
Loads of stuff has been installed in the top level directory. Once the downloaded files are cached in your local Maven repository, it won't take long to run this for every build.
50
+
41
51
## Install Angular CLI
42
52
53
+
To build an Angular app these days it really helps to use the CLI provided by the Angular team. We can install it using the `npm` that we just got using the plugin. First create a convenient script to run `npm` from the local installation (in case you have others on your path):
54
+
43
55
```
44
56
$ cat > npm
45
57
#!/bin/sh
46
58
cd $(dirname $0)
47
59
PATH="$PWD/node/":$PATH
48
60
node "node/node_modules/npm/bin/npm-cli.js" "$@"
49
61
$ chmod +x npm
62
+
```
63
+
64
+
and then run it to install the CLI:
65
+
66
+
```
50
67
$ ./npm install @angular/cli
51
68
```
52
69
53
-
and run `mvn generate-resources` again.
70
+
Then create a similar wrapper for the CLI itself, and test it quickly:
54
71
55
72
```
56
73
$ cat > ng
@@ -73,6 +90,8 @@ os: linux x64
73
90
74
91
## Create an Angular App
75
92
93
+
The Angular CLI can be used to generate new application scaffolding, as well as other things. It's a useful starting point, but you could at this point grab any existing Angular app and put it in the same place. We want to work with the Angular app in a subdirectory of `src/main`, just to keep the source code tidy and make it look like a Maven build.
94
+
76
95
Create the app with the CLI and move it to `src/main`:
77
96
78
97
```
@@ -83,28 +102,25 @@ $ sed -i -e 's,dist,../../../target/classes/static,' src/main/client/.angular-cl
83
102
$ mv ng npm src/main/client
84
103
```
85
104
105
+
We discarded the node modules that the CLI installed because we want the frontend plugin to do that work for us in an automated build. We also edited the `.angular-cli.json` (a bit like a `pom.cxml` for the Angular CLI app) to point the output from the ANgular build to a location that will be packaged in our JAR file.
and add an execution to install the modules used in the application:
97
116
98
117
```
99
-
<execution>
100
-
<id>npm-install</id>
101
-
<goals>
102
-
<goal>npm</goal>
103
-
</goals>
104
-
<configuration>
105
-
<arguments>install</arguments>
106
-
</configuration>
107
-
</execution>
118
+
<execution>
119
+
<id>npm-install</id>
120
+
<goals>
121
+
<goal>npm</goal>
122
+
</goals>
123
+
</execution>
108
124
```
109
125
110
126
Install the modules again using `mvn generate-resources`.
@@ -135,7 +151,7 @@ os: linux x64
135
151
typescript: 2.3.4
136
152
```
137
153
138
-
And the tests work:
154
+
At this point, the tests work:
139
155
140
156
```
141
157
$ src/main/client/ng e2e
@@ -151,7 +167,7 @@ Executed 1 of 1 spec SUCCESS in 0.718 sec.
151
167
[13:59:48] I/launcher - chrome #01 passed
152
168
```
153
169
154
-
Add
170
+
and if you add this as well:
155
171
156
172
```
157
173
<execution>
@@ -165,19 +181,23 @@ Add
165
181
</execution>
166
182
```
167
183
168
-
and then the client app will be compiled during the Maven build.
184
+
then the client app will be compiled during the Maven build.
185
+
186
+
## Development Time
169
187
170
188
You can build continuously with
171
189
172
190
```
173
191
$ src/main/client/ng build --watch
174
192
```
175
193
176
-
Updates are built (quickly) and pushed to `target/classes` where they can be picked up by Spring Boot.
194
+
Updates are built (quickly) and pushed to `target/classes` where they can be picked up by Spring Boot. Your IDE might need to be tweaked to pick up the changes automatically (Spring Tool Suite does it out of the box).
195
+
196
+
That's it really, but we can quickly look into a couple of extra things that will get you off the ground quickly with Spring Boot and Angular.
You can add basic Twitter Bootstrap features to make the app look a bit less dull (taken from [this blog](https://medium.com/codingthesmartway-com-blog/using-bootstrap-with-angular-c83c3cee3f4a)):
Some basic features are included in the default scaffolding app, including the HTTP client, HTML forms support and navigation using the `Router`. All of them are extremely well documented at [angular.io](https://angular.io), and there are thousands of examples out in the internet of how to use those features.
222
+
223
+
As an example, lets look at how to add an HTTP Client call, and hook it up to a Spring `@RestController`. In the front end `app-root` component we can add some placeholders for dynamic content:
224
+
225
+
app.component.html:
226
+
```html
227
+
<divstyle="text-align:center"class="container">
228
+
<h1>
229
+
Welcome {{title}}!
230
+
</h1>
231
+
<divclass="container">
232
+
<p>Id: <span>{{data.id}}</span></p>
233
+
<p>Message: <span>{{data.content}}</span></p>
234
+
</div>
235
+
</div>
236
+
```
237
+
238
+
so we are looking for a `data` object in the scope of the component:
Notice how the `AppComponent` has an `HttpClient` injected into its constructor. In the module definition we need to import the `HttpClientModule` as well, to enable the dependency injection:
0 commit comments