Skip to content

Commit ae525f5

Browse files
committed
Move to Asciidoctor
1 parent 8b4178b commit ae525f5

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

Diff for: README.md renamed to README.adoc

+23-21
Original file line numberDiff line numberDiff 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
22

33
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.
44

55
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.
66

7-
## Create a Spring Boot Application
7+
== Create a Spring Boot Application
88

99
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:
1010

@@ -15,10 +15,11 @@ $ ./mvnw install
1515

1616
Now we'll take that application and add some Angular scaffolding. Before we can do anything with Angular, we have to install `npm`.
1717

18-
## Install Npm Locally
18+
== Install Npm Locally
1919

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`:
2121

22+
.pom.xml
2223
```
2324
<build>
2425
<plugins>
@@ -55,7 +56,7 @@ $ ls node*
5556

5657
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.
5758

58-
## Install Angular CLI
59+
== Install Angular CLI
5960

6061
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):
6162

@@ -95,7 +96,7 @@ node: 8.8.1
9596
os: linux x64
9697
```
9798

98-
## Create an Angular App
99+
== Create an Angular App
99100

100101
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.
101102

@@ -114,7 +115,7 @@ $ sed -i -e 's,dist,target/classes/static,' .angular-cli.json
114115

115116
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.
116117

117-
## Building
118+
== Building
118119

119120
Add an execution to install the modules used in the application:
120121

@@ -131,7 +132,7 @@ Install the modules again using `./mvnw generate-resources` and check the result
131132

132133
```
133134
$ ./ng version
134-
_ _ ____ _ ___
135+
_ _ ____ _ ___
135136
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
136137
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
137138
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
@@ -187,7 +188,7 @@ and if you add this as well:
187188

188189
then the client app will be compiled during the Maven build.
189190

190-
### Stabilize the Build
191+
=== Stabilize the Build
191192

192193
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:
193194

@@ -199,7 +200,7 @@ If you want a stable build you should put a `^` before the version of `@angular/
199200
...
200201
```
201202

202-
## Development Time
203+
== Development Time
203204

204205
You can build continuously with
205206

@@ -211,7 +212,7 @@ Updates are built (quickly) and pushed to `target/classes` where they can be pic
211212

212213
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.
213214

214-
### VSCode
215+
=== VSCode
215216

216217
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.
217218

@@ -237,9 +238,9 @@ What VSCode doesn't have currently is automatic detection of `npm` build tools i
237238

238239
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.
239240

240-
## Adding Bootstrap
241+
== Adding Bootstrap
241242

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]):
243244

244245
```
245246
$ ./npm install bootstrap@3 jquery --save
@@ -271,14 +272,15 @@ Here's a magic command line to do that:
271272

272273
```
273274
$ cat .angular-cli.json | jq '.apps[0].styles[1] |= . + "../node_modules/bootstrap/dist/css/bootstrap.min.css"' | jq '.apps[0] += {scripts:["../node_modules/jquery/dist/jquery.min.js","../node_modules/bootstrap/dist/js/bootstrap.min.js"]}' > .tmp && mv .tmp .angular-cli.json
275+
```
274276

275-
## Basic Angular Features
277+
== Basic Angular Features
276278

277-
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.
278280

279281
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:
280282

281-
app.component.html:
283+
.app.component.html:
282284
```html
283285
<div style="text-align:center"class="container">
284286
<h1>
@@ -293,7 +295,7 @@ app.component.html:
293295

294296
so we are looking for a `data` object in the scope of the component:
295297

296-
app.component.ts:
298+
.app.component.ts:
297299
```javascript
298300
import { Component } from '@angular/core';
299301
import {HttpClient} from '@angular/common/http';
@@ -314,7 +316,7 @@ export class AppComponent {
314316

315317
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:
316318

317-
app.module.ts
319+
.app.module.ts
318320
```javascript
319321
import { BrowserModule } from '@angular/platform-browser';
320322
import { NgModule } from '@angular/core';
@@ -338,7 +340,7 @@ export class AppModule { }
338340

339341
In our Spring Boot application we need to service the `/resource` request and return an object with the right keys for the client:
340342

341-
DemoApplication.java:
343+
.DemoApplication.java:
342344
```java
343345
@SpringBootApplication
344346
@Controller
@@ -358,8 +360,8 @@ public class DemoApplication {
358360
}
359361
```
360362

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.
362364

363-
## Conclusion
365+
== Conclusion
364366

365367
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

Comments
 (0)