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
Copy file name to clipboardExpand all lines: README.adoc
+23-21
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
-
#Creating a New Application with Spring Boot and Angular
1
+
=Creating a New Application with Spring Boot and Angular
2
2
3
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
4
5
5
This article shows you how to do that and keep your Spring Boot application intact. 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
6
7
-
##Create a Spring Boot Application
7
+
==Create a Spring Boot Application
8
8
9
9
Whatever you normally do to create a new Spring Boot application, do that. For example you could use your IDE features. Or you could do it on the command line:
10
10
@@ -15,10 +15,11 @@ $ ./mvnw install
15
15
16
16
Now we'll take that application and add some Angular scaffolding. Before we can do anything with Angular, we have to install `npm`.
17
17
18
-
##Install Npm Locally
18
+
== Install Npm Locally
19
19
20
-
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`:
20
+
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 https://github.com/eirslett/frontend-maven-plugin[Maven Frontend Plugin] from Eirik Sletteberg. The first step is to add it to our `pom.xml`:
21
21
22
+
.pom.xml
22
23
```
23
24
<build>
24
25
<plugins>
@@ -55,7 +56,7 @@ $ ls node*
55
56
56
57
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.
57
58
58
-
##Install Angular CLI
59
+
== Install Angular CLI
59
60
60
61
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):
61
62
@@ -95,7 +96,7 @@ node: 8.8.1
95
96
os: linux x64
96
97
```
97
98
98
-
##Create an Angular App
99
+
== Create an Angular App
99
100
100
101
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 the top level directory to keep all the tools and IDEs happy, but we also want make it look like a regular Maven build.
101
102
@@ -114,7 +115,7 @@ $ sed -i -e 's,dist,target/classes/static,' .angular-cli.json
114
115
115
116
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.xml` for the Angular CLI app) to point the output from the Angular build to a location that will be packaged in our JAR file.
116
117
117
-
##Building
118
+
== Building
118
119
119
120
Add an execution to install the modules used in the application:
120
121
@@ -131,7 +132,7 @@ Install the modules again using `./mvnw generate-resources` and check the result
then the client app will be compiled during the Maven build.
189
190
190
-
###Stabilize the Build
191
+
=== Stabilize the Build
191
192
192
193
If you want a stable build you should put a `^` before the version of `@angular/cli` in your `package.json`. It isn't added by default when you do `ng new`, but it protects you from changes in the CLI. Example:
193
194
@@ -199,7 +200,7 @@ If you want a stable build you should put a `^` before the version of `@angular/
199
200
...
200
201
```
201
202
202
-
##Development Time
203
+
== Development Time
203
204
204
205
You can build continuously with
205
206
@@ -211,7 +212,7 @@ Updates are built (quickly) and pushed to `target/classes` where they can be pic
211
212
212
213
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.
213
214
214
-
###VSCode
215
+
=== VSCode
215
216
216
217
https://code.visualstudio.com/[Microsoft VSCode] is quite a good tool for developing JavaScript applications, and it also has good support for Java and Spring Boot. If you install the "Java Extension Pack" (from Microsoft), the "Angular Essentials" (from Jon Papa) and the "Latest TypeScript and JavaScript Grammar" (from Microsoft) you will be able to do code completion and source navigation in the Angular app (all those extensions and discoverable from the IDE). There are also some Spring Boot features that you need to download and install (in Extensions view click on top right and choose `Install from VSIX...`) at http://dist.springsource.com/snapshot/STS4/nightly-distributions.html.
217
218
@@ -237,9 +238,9 @@ What VSCode doesn't have currently is automatic detection of `npm` build tools i
237
238
238
239
With that in place your `Tasks->Run Task...` menu should include the `ng-watch` option, and it will run the angular build for you and re-compile if you make changes. You could add other entries for running tests.
239
240
240
-
##Adding Bootstrap
241
+
== Adding Bootstrap
241
242
242
-
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)):
243
+
You can add basic Twitter Bootstrap features to make the app look a bit less dull (taken from https://medium.com/codingthesmartway-com-blog/using-bootstrap-with-angular-c83c3cee3f4a[this blog]):
243
244
244
245
```
245
246
$ ./npm install bootstrap@3 jquery --save
@@ -271,14 +272,15 @@ Here's a magic command line to do that:
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.
279
+
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 https://angular.io[angular.io], and there are thousands of examples out in the internet of how to use those features.
278
280
279
281
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:
280
282
281
-
app.component.html:
283
+
.app.component.html:
282
284
```html
283
285
<div style="text-align:center"class="container">
284
286
<h1>
@@ -293,7 +295,7 @@ app.component.html:
293
295
294
296
so we are looking for a `data` object in the scope of the component:
295
297
296
-
app.component.ts:
298
+
.app.component.ts:
297
299
```javascript
298
300
import { Component } from '@angular/core';
299
301
import {HttpClient} from '@angular/common/http';
@@ -314,7 +316,7 @@ export class AppComponent {
314
316
315
317
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:
316
318
317
-
app.module.ts
319
+
.app.module.ts
318
320
```javascript
319
321
import { BrowserModule } from '@angular/platform-browser';
320
322
import { NgModule } from '@angular/core';
@@ -338,7 +340,7 @@ export class AppModule { }
338
340
339
341
In our Spring Boot application we need to service the `/resource` request and return an object with the right keys for the client:
340
342
341
-
DemoApplication.java:
343
+
.DemoApplication.java:
342
344
```java
343
345
@SpringBootApplication
344
346
@Controller
@@ -358,8 +360,8 @@ public class DemoApplication {
358
360
}
359
361
```
360
362
361
-
If you look at the source code [in Github](https://github.com/dsyer/spring-boot-angular)you will also notice that there is a test for the backend interaction in `app.component.spec.ts` (thanks to [this Ninja Squad blog](http://blog.ninja-squad.com/2017/07/17/http-client-module/)). The `pom.xml` has been modified to run the Angular e2e tests at the same time as the Java tests.
363
+
If you look at the source code https://github.com/dsyer/spring-boot-angular[in Github] you will also notice that there is a test for the backend interaction in `app.component.spec.ts` (thanks to http://blog.ninja-squad.com/2017/07/17/http-client-module/[this Ninja Squad blog]). The `pom.xml` has been modified to run the Angular e2e tests at the same time as the Java tests.
362
364
363
-
##Conclusion
365
+
== Conclusion
364
366
365
367
We have created a Spring Boot application, added a simple HTTP endpoint to it, and then added a front end to it using Angular. The Angular app is self-contained, so anyone who knows the tools can work with it from its own directory. The Spring Boot application folds the Angular assets into its build and a developer can easily update and test the front end from a regular IDE by running the app in the usual way.
0 commit comments