Przeglądaj źródła

Convert to asciidoctor

Greg Turnquist 13 lat temu
rodzic
commit
a550c3c64e
3 zmienionych plików z 120 dodań i 626 usunięć
  1. 1
    0
      .gitignore
  2. 119
    115
      README.adoc
  3. 0
    511
      README.md

+ 1
- 0
.gitignore Wyświetl plik

@@ -12,3 +12,4 @@ dependency-reduced-pom.xml
12 12
 *.sublime-*
13 13
 /scratch
14 14
 .gradle
15
+README.html

README.ftl.md → README.adoc Wyświetl plik

@@ -1,57 +1,72 @@
1
-<#assign project_id="gs-spring-boot">
2
-This guide provides a sampling of how [Spring Boot][spring-boot] helps you accelerate and facilitate application development. As you read more Spring Getting Started guides, you will see more use cases for Spring Boot.
3
-
4
-What you'll build
5
------------------
1
+:spring_boot_version: 0.5.0.M6
2
+:spring-boot: https://github.com/spring-projects/spring-boot
3
+:toc:
4
+:icons: font
5
+:source-highlighter: prettify
6
+:project_id: gs-spring-boot
7
+This guide provides a sampling of how {spring-boot}[Spring Boot] helps you accelerate and facilitate application development. As you read more Spring Getting Started guides, you will see more use cases for Spring Boot.
8
+
9
+== What you'll build
6 10
 You'll build a simple web application with Spring Boot and add some useful services to it.
7 11
 
8
-What you'll need
9
-----------------
12
+== What you'll need
13
+
14
+include::https://raw.github.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[]
10 15
 
11
- - About 15 minutes
12
- - <@prereq_editor_jdk_buildtools/>
16
+:jump_ahead: Learn what you can do with Spring Boot
17
+include::https://raw.github.com/spring-guides/getting-started-macros/master/how_to_complete_this_guide.adoc[]
13 18
 
14
-## <@how_to_complete_this_guide jump_ahead='Learn what you can do with Spring Boot'/>
15 19
 
20
+[[scratch]]
21
+== Set up the project
16 22
 
17
-<a name="scratch"></a>
18
-Set up the project
19
-------------------
23
+include::https://raw.github.com/spring-guides/getting-started-macros/master/build_system_intro.adoc[]
20 24
 
21
-<@build_system_intro/>
25
+include::https://raw.github.com/spring-guides/getting-started-macros/master/create_directory_structure_hello.adoc[]
22 26
 
23
-<@create_directory_structure_hello/>
24 27
 
28
+include::https://raw.github.com/spring-guides/getting-started-macros/master/create_both_builds.adoc[]
25 29
 
26
-<@create_both_builds/>
30
+`build.gradle`
31
+// AsciiDoc source formatting doesn't support groovy, so using java instead
32
+[source,java]
33
+----
34
+include::initial/build.gradle[]
35
+----
27 36
 
28
-Learn what you can do with Spring Boot
29
---------------------------------------
37
+== Learn what you can do with Spring Boot
30 38
 
31 39
 Spring Boot offers a fast way to build applications. It looks at your classpath and at beans you have configured, makes reasonable assumptions about what you're missing, and adds it. With Spring Boot you can focus more on business features and less on infrastructure.
32 40
 
33 41
 For example:
42
+
34 43
 - Got Spring MVC? There are several specific beans you almost always need, and Spring Boot adds them automatically. A Spring MVC app also needs a servlet container, so Spring Boot automatically configures embedded Tomcat.
35 44
 - Got Jetty? If so, you probably do NOT want Tomcat, but instead embedded Jetty. Spring Boot handles that for you.
36 45
 - Got Thymeleaf? There are a few beans that must always be added to your application context; Spring Boot adds them for you.
37 46
 
38 47
 These are just a few examples of the automatic configuration Spring Boot provides. At the same time, Spring Boot doesn't get in your way. For example, if Thymeleaf is on your path, Spring Boot adds a `SpringTemplateEngine` to your application context automatically. But if you define your own `SpringTemplateEngine` with your own settings, then Spring Boot won't add one. This leaves you in control with little effort on your part.
39 48
 
40
-> **Note:** Spring Boot doesn't generate code or make edits to your files. Instead, when you start up your application, Spring Boot dynamically wires up beans and settings and applies them to your application context.
49
+NOTE: Spring Boot doesn't generate code or make edits to your files. Instead, when you start up your application, Spring Boot dynamically wires up beans and settings and applies them to your application context.
41 50
 
42
-Create a simple web application
43
----------------------------------
51
+== Create a simple web application
44 52
 Now you can create a web controller for a simple web application.
45 53
 
46
-    <@snippet path="src/main/java/hello/HelloController.java" prefix="initial"/>
54
+`src/main/java/hello/HelloController.java`
55
+[source,java]
56
+----
57
+include::initial/src/main/java/hello/HelloController.java[]
58
+----
47 59
     
48 60
 The class is flagged as a `@RestController`, meaning it's ready for use by Spring MVC to handle web requests. `@RequestMapping` maps `/` to the `index()` method. When invoked from a browser or using curl on the command line, the method returns pure text. That's because `@RestController` combines `@Controller` and `@ResponseBody`, two annotations that results in web requests returning data rather than a view.
49 61
 
50
-Create an Application class
51
----------------------------
62
+== Create an Application class
52 63
 Here you create an `Application` class with the components:
53 64
 
54
-    <@snippet path="src/main/java/hello/Application.java" prefix="initial"/>
65
+`src/main/java/hello/Application.java`
66
+[source,java]
67
+----
68
+include::initial/src/main/java/hello/Application.java[]
69
+----
55 70
     
56 71
 - `@Configuration` tags the class as a source of bean definitions for the application context.
57 72
 - `@EnableAutoConfiguration` tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
@@ -62,23 +77,22 @@ The `main()` method uses Spring Boot's `SpringApplication.run()` method to launc
62 77
 
63 78
 The `run()` method returns an `ApplicationContext` and this application then retrieves all the beans that were created either by your app or were automatically added thanks to Spring Boot. It sorts them and prints them out.
64 79
 
65
-Run the application
66
--------------------
80
+== Run the application
67 81
 To run the application, execute:
68 82
 
69
-```sh
70
-$ ./gradlew build && java -jar build/libs/${project_id}-0.1.0.jar
71
-```
83
+----
84
+./gradlew build && java -jar build/libs/{project_id}-0.1.0.jar
85
+----
72 86
 
73 87
 If you are using Maven, execute:
74 88
 
75
-```sh
76
-$ mvn package && java -jar target/${project_id}-0.1.0.jar
77
-```
89
+----
90
+mvn package && java -jar target/{project_id}-0.1.0.jar
91
+----
78 92
 
79 93
 You should see some output like this:
80 94
 
81
-```
95
+....
82 96
 Let's inspect the beans provided by Spring Boot:
83 97
 application
84 98
 beanNameHandlerMapping
@@ -114,33 +128,34 @@ resourceHandlerMapping
114 128
 simpleControllerHandlerAdapter
115 129
 tomcatEmbeddedServletContainerFactory
116 130
 viewControllerHandlerMapping
117
-```
131
+....
118 132
 
119 133
 You can clearly see **org.springframework.boot.autoconfigure** beans. There is also a `tomcatEmbeddedServletContainerFactory`.
120 134
 
121 135
 Check out the service.
122 136
 
123
-```sh
137
+....
124 138
 $ curl localhost:8080
125 139
 Greetings from Spring Boot!
126
-```
140
+....
127 141
 
128
-Switch from Tomcat to Jetty
129
----------------------------
142
+== Switch from Tomcat to Jetty
130 143
 What if you prefer Jetty over Tomcat? Jetty and Tomcat are both compliant servlet containers, so it should be easy to switch. With Spring Boot, it is!
131 144
 
132 145
 Change your `build.gradle` to exclude Tomcat then add Jetty to the list of dependencies:
133 146
 
134
-```groovy
135
-    compile("org.springframework.boot:spring-boot-starter-web:0.5.0.M4") {
147
+[source,groovy]
148
+----
149
+    compile("org.springframework.boot:spring-boot-starter-web:0.5.0.M6") {
136 150
         exclude module: "spring-boot-starter-tomcat"
137 151
     }
138
-    compile("org.springframework.boot:spring-boot-starter-jetty:0.5.0.M4")
139
-```
152
+    compile("org.springframework.boot:spring-boot-starter-jetty:0.5.0.M6")
153
+----
140 154
 
141 155
 If you are using Maven, the changes look like this:
142 156
 
143
-```xml
157
+[source,xml]
158
+----
144 159
         <dependency>
145 160
             <groupId>org.springframework.boot</groupId>
146 161
             <artifactId>spring-boot-starter-web</artifactId>
@@ -155,33 +170,36 @@ If you are using Maven, the changes look like this:
155 170
             <groupId>org.springframework.boot</groupId>
156 171
             <artifactId>spring-boot-starter-jetty</artifactId>
157 172
         </dependency>
158
-```
173
+----
159 174
 
160 175
 This change isn't about comparing Tomcat vs. Jetty. Instead, it demonstrates how Spring Boot reacts to what is on your classpath.
161 176
 
162 177
 As you can see below, the code is the same as before:
163 178
 
164
-    <@snippet path="src/main/java/hello/Application.java" prefix="complete"/>
179
+`src/main/java/hello/Application.java`
180
+[source,java]
181
+----
182
+include::complete/src/main/java/hello/Application.java[]
183
+----
165 184
     
166 185
 
167
-Re-run the application
168
-----------------------
186
+== Re-run the application
169 187
 
170 188
 Run the app again:
171 189
 
172
-```sh
173
-$ ./gradlew build && java -jar build/libs/${project_id}-0.1.0.jar
174
-```
190
+----
191
+./gradlew build && java -jar build/libs/{project_id}-0.1.0.jar
192
+----
175 193
 
176 194
 If you are using Maven, execute:
177 195
 
178
-```sh
179
-$ mvn package && java -jar target/${project_id}-0.1.0.jar
180
-```
196
+----
197
+mvn package && java -jar target/{project_id}-0.1.0.jar
198
+----
181 199
 
182 200
 Now check out the output:
183 201
 
184
-```
202
+....
185 203
 Let's inspect the beans provided by Spring Boot:
186 204
 application
187 205
 beanNameHandlerMapping
@@ -223,46 +241,49 @@ requestMappingHandlerMapping
223 241
 resourceHandlerMapping
224 242
 simpleControllerHandlerAdapter
225 243
 viewControllerHandlerMapping
226
-```
244
+....
227 245
 
228 246
 There is little change from the previous output, except there is no longer a `tomcatEmbeddedServletContainerFactory`. Instead, there is a new `jettyEmbeddedServletContainer`. 
229 247
 
230 248
 Otherwise, everything is the same, as it should be. Most beans listed above provide Spring MVC's production-grade features. Simply swapping one part, the servlet container, shouldn't cause a system-wide ripple.
231 249
 
232
-Add production-grade services
233
-------------------------------
234
-If you are building a web site for your business, you probably need to add some management services. Spring Boot provides several out of the box with its [actuator module][spring-boot-actuator], such as health, audits, beans, and more.
250
+== Add production-grade services
251
+If you are building a web site for your business, you probably need to add some management services. Spring Boot provides several out of the box with its https://github.com/spring-projects/spring-boot/blob/master/spring-boot-actuator/README.md[actuator module], such as health, audits, beans, and more.
235 252
 
236 253
 Add this to your build file's list of dependencies:
237 254
 
238
-```groovy
239
-    compile("org.springframework.boot:spring-boot-starter-actuator:0.5.0.M4")
240
-```
255
+[source,groovy]
256
+----
257
+    compile("org.springframework.boot:spring-boot-starter-actuator:0.5.0.M6")
258
+----
241 259
 
242 260
 If you are using Maven, add this to your list of dependencies:
243 261
 
244
-```xml
262
+[source,xml]
263
+----
245 264
         <dependency>
246 265
             <groupId>org.springframework.boot</groupId>
247 266
             <artifactId>spring-boot-starter-actuator</artifactId>
248 267
         </dependency>
249
-```
268
+----
250 269
 
251 270
 Then restart the app:
252 271
 
253
-```sh
254
-$ ./gradlew build && java -jar build/libs/${project_id}-0.1.0.jar
255
-```
272
+[subs="attributes"]
273
+----
274
+./gradlew build && java -jar build/libs/{project_id}-0.1.0.jar
275
+----
256 276
 
257 277
 If you are using Maven, execute:
258 278
 
259
-```sh
260
-$ mvn package && java -jar target/${project_id}-0.1.0.jar
261
-```
279
+[subs="attributes"]
280
+----
281
+mvn package && java -jar target/{project_id}-0.1.0.jar
282
+----
262 283
 
263 284
 You will see a new set of RESTful end points added to the application. These are management services provided by Spring Boot.
264 285
 
265
-```
286
+....
266 287
 2013-08-01 08:03:42.592  INFO 43851 ... Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.Map<java.lang.String, java.lang.Object> org.springframework.boot.ops.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
267 288
 2013-08-01 08:03:42.592  INFO 43851 ... Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.ops.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
268 289
 2013-08-01 08:03:42.844  INFO 43851 ... Mapped URL path [/env] onto handler of type [class org.springframework.boot.ops.endpoint.EnvironmentEndpoint]
@@ -273,49 +294,36 @@ You will see a new set of RESTful end points added to the application. These are
273 294
 2013-08-01 08:03:42.845  INFO 43851 ... Mapped URL path [/trace] onto handler of type [class org.springframework.boot.ops.endpoint.TraceEndpoint]
274 295
 2013-08-01 08:03:42.845  INFO 43851 ... Mapped URL path [/dump] onto handler of type [class org.springframework.boot.ops.endpoint.DumpEndpoint]
275 296
 2013-08-01 08:03:42.845  INFO 43851 ... Mapped URL path [/shutdown] onto handler of type [class org.springframework.boot.ops.endpoint.ShutdownEndpoint]
276
-```
297
+....
277 298
 
278
-They include: errors, [environment](http://localhost:8080/env), [health](http://localhost:8080/health), [beans](http://localhost:8080/beans), [info](http://localhost:8080/info), [metrics](http://localhost:8080/metrics), [trace](http://localhost:8080/trace), [dump](http://localhost:8080/dump), and shutdown.
299
+They include: errors, http://localhost:8080/env[environment], http://localhost:8080/health[health], http://localhost:8080/beans[beans], http://localhost:8080/info[info], http://localhost:8080/metrics[metrics], http://localhost:8080/trace[trace], http://localhost:8080/dump[dump], and shutdown.
279 300
 
280 301
 It's easy to check the health of the app.
281 302
 
282
-```sh
303
+----
283 304
 $ curl localhost:8080/health
284 305
 ok
285
-```
306
+----
286 307
 
287 308
 You can invoke shutdown through curl.
288 309
 
289
-```sh
310
+----
290 311
 $ curl -X POST localhost:8080/shutdown
291
-```
312
+----
292 313
 
293 314
 The response shows that shutdown through REST is currently disabled by default:
294
-```sh
315
+----
295 316
 {"message":"Shutdown not enabled, sorry."}
296
-```
317
+----
297 318
 
298 319
 Whew! You probably don't want that until you are ready to turn on proper security settings, if at all.
299 320
 
300
-For more details about each of these REST points and how you can tune their settings with an `application.properties` file, check out the [Spring Boot][spring-boot] project.
301
-
302
-View Spring Boot's starters
303
-----------------------
304
-You have seen some of Spring Boot's **starters**. Here is a complete list:
305
-- spring-boot-starter-actuator
306
-- spring-boot-starter-batch
307
-- spring-boot-starter-data-jpa
308
-- spring-boot-starter-integration
309
-- spring-boot-starter-jetty
310
-- spring-boot-starter-logging
311
-- spring-boot-starter-parent
312
-- spring-boot-starter-security
313
-- spring-boot-starter-tomcat
314
-- spring-boot-starter-web
315
-
316
-
317
-JAR support and Groovy support
318
-------------------------------
321
+For more details about each of these REST points and how you can tune their settings with an `application.properties` file, check out the {spring-boot}[Spring Boot] project.
322
+
323
+== View Spring Boot's starters
324
+You have seen some of Spring Boot's **starters**. You can see them all https://github.com/spring-projects/spring-boot/tree/master/spring-boot-starters[here].
325
+
326
+== JAR support and Groovy support
319 327
 The last example showed how Spring Boot makes it easy to wire beans you may not be aware that you need. And it showed how to turn on convenient management services.
320 328
 
321 329
 But Spring Boot does yet more. It supports not only traditional WAR file deployments, but also makes it easy to put together executable JARs thanks to Spring Boot's loader module. The various guides demonstrate this dual support through the `spring-boot-gradle-plugin` and `spring-boot-maven-plugin`.
@@ -324,7 +332,8 @@ On top of that, Spring Boot also has Groovy support, allowing you to build Sprin
324 332
 
325 333
 Create a new file called **app.groovy** and put the following code in it:
326 334
 
327
-```groovy
335
+[source,groovy]
336
+----
328 337
 @RestController
329 338
 class ThisWillActuallyRun {
330 339
 
@@ -334,32 +343,27 @@ class ThisWillActuallyRun {
334 343
     }
335 344
 
336 345
 }
337
-```
346
+----
338 347
 
339
-> **Note:** It doesn't matter where the file is. You can even fit an application that small inside a [single tweet](https://twitter.com/rob_winch/status/364871658483351552)!
348
+NOTE: It doesn't matter where the file is. You can even fit an application that small inside a https://twitter.com/rob_winch/status/364871658483351552[single tweet]!
340 349
 
341
-Next, [install Spring Boot's CLI](https://github.com/spring-projects/spring-boot#installing-the-cli).
350
+Next, https://github.com/spring-projects/spring-boot#installing-the-cli[install Spring Boot's CLI].
342 351
 
343 352
 Run it as follows:
344 353
 
345
-```sh
354
+----
346 355
 $ spring run app.groovy
347
-```
356
+----
348 357
 
349
-> **Note:** This assumes you shut down the previous application, to avoid a port collision.
358
+NOTE: This assumes you shut down the previous application, to avoid a port collision.
350 359
 
351 360
 From a different terminal window:
352
-```sh
361
+----
353 362
 $ curl localhost:8080
354 363
 Hello World!
355
-```
356
-
357
-Spring Boot does this by dynamically adding key annotations to your code and leveraging [Groovy Grapes](http://groovy.codehaus.org/Grape) to pull down needed libraries to make the app run.
364
+----
358 365
 
359
-Summary
360
-----------------
361
-Congratulations! You built a simple web application with Spring Boot and learned how it can ramp up your development pace. You also turned on some handy production services.
366
+Spring Boot does this by dynamically adding key annotations to your code and leveraging http://groovy.codehaus.org/Grape[Groovy Grapes] to pull down needed libraries to make the app run.
362 367
 
363
-[spring-boot]: https://github.com/spring-projects/spring-boot
364
-[spring-boot-actuator]: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-actuator/README.md
365
-[gs-uploading-files]: /guides/gs/uploading-files
368
+== Summary
369
+Congratulations! You built a simple web application with Spring Boot and learned how it can ramp up your development pace. You also turned on some handy production services.

+ 0
- 511
README.md Wyświetl plik

@@ -1,511 +0,0 @@
1
-This guide provides a sampling of how [Spring Boot][spring-boot] helps you accelerate and facilitate application development. As you read more Spring Getting Started guides, you will see more use cases for Spring Boot.
2
-
3
-What you'll build
4
------------------
5
-You'll build a simple web application with Spring Boot and add some useful services to it.
6
-
7
-What you'll need
8
-----------------
9
-
10
- - About 15 minutes
11
- - A favorite text editor or IDE
12
- - [JDK 6][jdk] or later
13
- - [Gradle 1.8+][gradle] or [Maven 3.0+][mvn]
14
- - You can also import the code from this guide as well as view the web page directly into [Spring Tool Suite (STS)][gs-sts] and work your way through it from there.
15
-
16
-[jdk]: http://www.oracle.com/technetwork/java/javase/downloads/index.html
17
-[gradle]: http://www.gradle.org/
18
-[mvn]: http://maven.apache.org/download.cgi
19
-[gs-sts]: /guides/gs/sts
20
-
21
-How to complete this guide
22
---------------------------
23
-
24
-Like all Spring's [Getting Started guides](/guides/gs), you can start from scratch and complete each step, or you can bypass basic setup steps that are already familiar to you. Either way, you end up with working code.
25
-
26
-To **start from scratch**, move on to [Set up the project](#scratch).
27
-
28
-To **skip the basics**, do the following:
29
-
30
- - [Download][zip] and unzip the source repository for this guide, or clone it using [Git][u-git]:
31
-`git clone https://github.com/spring-guides/gs-spring-boot.git`
32
- - cd into `gs-spring-boot/initial`.
33
- - Jump ahead to [Learn what you can do with Spring Boot](#initial).
34
-
35
-**When you're finished**, you can check your results against the code in `gs-spring-boot/complete`.
36
-[zip]: https://github.com/spring-guides/gs-spring-boot/archive/master.zip
37
-[u-git]: /understanding/Git
38
-
39
-
40
-<a name="scratch"></a>
41
-Set up the project
42
-------------------
43
-
44
-First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with [Gradle](http://gradle.org) and [Maven](https://maven.apache.org) is included here. If you're not familiar with either, refer to [Building Java Projects with Gradle](/guides/gs/gradle/) or [Building Java Projects with Maven](/guides/gs/maven).
45
-
46
-### Create the directory structure
47
-
48
-In a project directory of your choosing, create the following subdirectory structure; for example, with `mkdir -p src/main/java/hello` on *nix systems:
49
-
50
-    └── src
51
-        └── main
52
-            └── java
53
-                └── hello
54
-
55
-
56
-### Create a Gradle build file
57
-Below is the [initial Gradle build file](https://github.com/spring-guides/gs-spring-boot/blob/master/initial/build.gradle). But you can also use Maven. The pom.xml file is included [right here](https://github.com/spring-guides/gs-spring-boot/blob/master/initial/pom.xml). If you are using [Spring Tool Suite (STS)][gs-sts], you can import the guide directly.
58
-
59
-`build.gradle`
60
-```gradle
61
-buildscript {
62
-    repositories {
63
-        maven { url "http://repo.spring.io/libs-snapshot" }
64
-        mavenLocal()
65
-    }
66
-    dependencies {
67
-        classpath("org.springframework.boot:spring-boot-gradle-plugin:0.5.0.M6")
68
-    }
69
-}
70
-
71
-apply plugin: 'java'
72
-apply plugin: 'eclipse'
73
-apply plugin: 'idea'
74
-apply plugin: 'spring-boot'
75
-
76
-jar {
77
-    baseName = 'gs-spring-boot'
78
-    version =  '0.1.0'
79
-}
80
-
81
-repositories {
82
-    mavenCentral()
83
-    maven { url "http://repo.spring.io/libs-snapshot" }
84
-}
85
-
86
-dependencies {
87
-    compile("org.springframework.boot:spring-boot-starter-web:0.5.0.M6")
88
-    testCompile("junit:junit:4.11")
89
-}
90
-
91
-task wrapper(type: Wrapper) {
92
-    gradleVersion = '1.8'
93
-}
94
-```
95
-    
96
-[gs-sts]: /guides/gs/sts    
97
-
98
-Learn what you can do with Spring Boot
99
---------------------------------------
100
-
101
-Spring Boot offers a fast way to build applications. It looks at your classpath and at beans you have configured, makes reasonable assumptions about what you're missing, and adds it. With Spring Boot you can focus more on business features and less on infrastructure.
102
-
103
-For example:
104
-- Got Spring MVC? There are several specific beans you almost always need, and Spring Boot adds them automatically. A Spring MVC app also needs a servlet container, so Spring Boot automatically configures embedded Tomcat.
105
-- Got Jetty? If so, you probably do NOT want Tomcat, but instead embedded Jetty. Spring Boot handles that for you.
106
-- Got Thymeleaf? There are a few beans that must always be added to your application context; Spring Boot adds them for you.
107
-
108
-These are just a few examples of the automatic configuration Spring Boot provides. At the same time, Spring Boot doesn't get in your way. For example, if Thymeleaf is on your path, Spring Boot adds a `SpringTemplateEngine` to your application context automatically. But if you define your own `SpringTemplateEngine` with your own settings, then Spring Boot won't add one. This leaves you in control with little effort on your part.
109
-
110
-> **Note:** Spring Boot doesn't generate code or make edits to your files. Instead, when you start up your application, Spring Boot dynamically wires up beans and settings and applies them to your application context.
111
-
112
-Create a simple web application
113
----------------------------------
114
-Now you can create a web controller for a simple web application.
115
-
116
-`src/main/java/hello/HelloController.java`
117
-```java
118
-package hello;
119
-
120
-import org.springframework.web.bind.annotation.RestController;
121
-import org.springframework.web.bind.annotation.RequestMapping;
122
-
123
-@RestController
124
-public class HelloController {
125
-    
126
-    @RequestMapping("/")
127
-    public String index() {
128
-        return "Greetings from Spring Boot!";
129
-    }
130
-    
131
-}
132
-```
133
-    
134
-The class is flagged as a `@RestController`, meaning it's ready for use by Spring MVC to handle web requests. `@RequestMapping` maps `/` to the `index()` method. When invoked from a browser or using curl on the command line, the method returns pure text. That's because `@RestController` combines `@Controller` and `@ResponseBody`, two annotations that results in web requests returning data rather than a view.
135
-
136
-Create an Application class
137
----------------------------
138
-Here you create an `Application` class with the components:
139
-
140
-`src/main/java/hello/Application.java`
141
-```java
142
-package hello;
143
-
144
-import java.util.Arrays;
145
-
146
-import org.springframework.boot.SpringApplication;
147
-import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
148
-import org.springframework.context.ApplicationContext;
149
-import org.springframework.context.annotation.ComponentScan;
150
-import org.springframework.context.annotation.Configuration;
151
-
152
-@Configuration
153
-@EnableAutoConfiguration
154
-@ComponentScan
155
-public class Application {
156
-    
157
-    public static void main(String[] args) {
158
-        ApplicationContext ctx = SpringApplication.run(Application.class, args);
159
-        
160
-        System.out.println("Let's inspect the beans provided by Spring Boot:");
161
-        
162
-        String[] beanNames = ctx.getBeanDefinitionNames();
163
-        Arrays.sort(beanNames);
164
-        for (String beanName : beanNames) {
165
-            System.out.println(beanName);
166
-        }
167
-    }
168
-
169
-}
170
-```
171
-    
172
-- `@Configuration` tags the class as a source of bean definitions for the application context.
173
-- `@EnableAutoConfiguration` tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
174
-- Normally you would add `@EnableWebMvc` for a Spring MVC app, but Spring Boot adds it automatically when it sees **spring-webmvc** on the classpath. This flags the application as a web application and activates key behaviors such as setting up a `DispatcherServlet`.
175
-- `@ComponentScanning` tells Spring to look for other components, configurations, and services in the the `hello` package, allowing it to find the `HelloController`.
176
-
177
-The `main()` method uses Spring Boot's `SpringApplication.run()` method to launch an application. Did you notice that there wasn't a single line of XML? No **web.xml** file either. This web application is 100% pure Java and you didn't have to deal with configuring any plumbing or infrastructure.
178
-
179
-The `run()` method returns an `ApplicationContext` and this application then retrieves all the beans that were created either by your app or were automatically added thanks to Spring Boot. It sorts them and prints them out.
180
-
181
-Run the application
182
--------------------
183
-To run the application, execute:
184
-
185
-```sh
186
-$ ./gradlew build && java -jar build/libs/gs-spring-boot-0.1.0.jar
187
-```
188
-
189
-If you are using Maven, execute:
190
-
191
-```sh
192
-$ mvn package && java -jar target/gs-spring-boot-0.1.0.jar
193
-```
194
-
195
-You should see some output like this:
196
-
197
-```
198
-Let's inspect the beans provided by Spring Boot:
199
-application
200
-beanNameHandlerMapping
201
-defaultServletHandlerMapping
202
-dispatcherServlet
203
-embeddedServletContainerCustomizerBeanPostProcessor
204
-handlerExceptionResolver
205
-helloController
206
-httpRequestHandlerAdapter
207
-messageSource
208
-mvcContentNegotiationManager
209
-mvcConversionService
210
-mvcValidator
211
-org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration
212
-org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
213
-org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration
214
-org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$DispatcherServletConfiguration
215
-org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat
216
-org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration
217
-org.springframework.boot.context.embedded.properties.ServerProperties
218
-org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor
219
-org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
220
-org.springframework.context.annotation.internalAutowiredAnnotationProcessor
221
-org.springframework.context.annotation.internalCommonAnnotationProcessor
222
-org.springframework.context.annotation.internalConfigurationAnnotationProcessor
223
-org.springframework.context.annotation.internalRequiredAnnotationProcessor
224
-org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration
225
-propertySourcesBinder
226
-propertySourcesPlaceholderConfigurer
227
-requestMappingHandlerAdapter
228
-requestMappingHandlerMapping
229
-resourceHandlerMapping
230
-simpleControllerHandlerAdapter
231
-tomcatEmbeddedServletContainerFactory
232
-viewControllerHandlerMapping
233
-```
234
-
235
-You can clearly see **org.springframework.boot.autoconfigure** beans. There is also a `tomcatEmbeddedServletContainerFactory`.
236
-
237
-Check out the service.
238
-
239
-```sh
240
-$ curl localhost:8080
241
-Greetings from Spring Boot!
242
-```
243
-
244
-Switch from Tomcat to Jetty
245
----------------------------
246
-What if you prefer Jetty over Tomcat? Jetty and Tomcat are both compliant servlet containers, so it should be easy to switch. With Spring Boot, it is!
247
-
248
-Change your `build.gradle` to exclude Tomcat then add Jetty to the list of dependencies:
249
-
250
-```groovy
251
-    compile("org.springframework.boot:spring-boot-starter-web:0.5.0.M4") {
252
-        exclude module: "spring-boot-starter-tomcat"
253
-    }
254
-    compile("org.springframework.boot:spring-boot-starter-jetty:0.5.0.M4")
255
-```
256
-
257
-If you are using Maven, the changes look like this:
258
-
259
-```xml
260
-        <dependency>
261
-            <groupId>org.springframework.boot</groupId>
262
-            <artifactId>spring-boot-starter-web</artifactId>
263
-            <exclusions>
264
-                <exclusion>
265
-                    <groupId>org.springframework.boot</groupId>
266
-                    <artifactId>spring-boot-starter-tomcat</artifactId>
267
-                </exclusion>
268
-            </exclusions>
269
-        </dependency>
270
-        <dependency>
271
-            <groupId>org.springframework.boot</groupId>
272
-            <artifactId>spring-boot-starter-jetty</artifactId>
273
-        </dependency>
274
-```
275
-
276
-This change isn't about comparing Tomcat vs. Jetty. Instead, it demonstrates how Spring Boot reacts to what is on your classpath.
277
-
278
-As you can see below, the code is the same as before:
279
-
280
-`src/main/java/hello/Application.java`
281
-```java
282
-package hello;
283
-
284
-import java.util.Arrays;
285
-
286
-import org.springframework.boot.SpringApplication;
287
-import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
288
-import org.springframework.context.ApplicationContext;
289
-import org.springframework.context.annotation.ComponentScan;
290
-import org.springframework.context.annotation.Configuration;
291
-
292
-@Configuration
293
-@EnableAutoConfiguration
294
-@ComponentScan
295
-public class Application {
296
-    
297
-    public static void main(String[] args) {
298
-        ApplicationContext ctx = SpringApplication.run(Application.class, args);
299
-        
300
-        System.out.println("Let's inspect the beans provided by Spring Boot:");
301
-        
302
-        String[] beanNames = ctx.getBeanDefinitionNames();
303
-        Arrays.sort(beanNames);
304
-        for (String beanName : beanNames) {
305
-            System.out.println(beanName);
306
-        }
307
-    }
308
-
309
-}
310
-```
311
-    
312
-
313
-Re-run the application
314
-----------------------
315
-
316
-Run the app again:
317
-
318
-```sh
319
-$ ./gradlew build && java -jar build/libs/gs-spring-boot-0.1.0.jar
320
-```
321
-
322
-If you are using Maven, execute:
323
-
324
-```sh
325
-$ mvn package && java -jar target/gs-spring-boot-0.1.0.jar
326
-```
327
-
328
-Now check out the output:
329
-
330
-```
331
-Let's inspect the beans provided by Spring Boot:
332
-application
333
-beanNameHandlerMapping
334
-defaultServletHandlerMapping
335
-dispatcherServlet
336
-embeddedServletContainerCustomizerBeanPostProcessor
337
-faviconHandlerMapping
338
-faviconRequestHandler
339
-handlerExceptionResolver
340
-helloController
341
-hiddenHttpMethodFilter
342
-httpRequestHandlerAdapter
343
-jettyEmbeddedServletContainerFactory
344
-messageSource
345
-mvcContentNegotiationManager
346
-mvcConversionService
347
-mvcValidator
348
-org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration
349
-org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
350
-org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration
351
-org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$DispatcherServletConfiguration
352
-org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$EmbeddedJetty
353
-org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration
354
-org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
355
-org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter
356
-org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter$FaviconConfiguration
357
-org.springframework.boot.context.embedded.properties.ServerProperties
358
-org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor
359
-org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
360
-org.springframework.context.annotation.internalAutowiredAnnotationProcessor
361
-org.springframework.context.annotation.internalCommonAnnotationProcessor
362
-org.springframework.context.annotation.internalConfigurationAnnotationProcessor
363
-org.springframework.context.annotation.internalRequiredAnnotationProcessor
364
-org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration
365
-propertySourcesBinder
366
-propertySourcesPlaceholderConfigurer
367
-requestMappingHandlerAdapter
368
-requestMappingHandlerMapping
369
-resourceHandlerMapping
370
-simpleControllerHandlerAdapter
371
-viewControllerHandlerMapping
372
-```
373
-
374
-There is little change from the previous output, except there is no longer a `tomcatEmbeddedServletContainerFactory`. Instead, there is a new `jettyEmbeddedServletContainer`. 
375
-
376
-Otherwise, everything is the same, as it should be. Most beans listed above provide Spring MVC's production-grade features. Simply swapping one part, the servlet container, shouldn't cause a system-wide ripple.
377
-
378
-Add production-grade services
379
-------------------------------
380
-If you are building a web site for your business, you probably need to add some management services. Spring Boot provides several out of the box with its [actuator module][spring-boot-actuator], such as health, audits, beans, and more.
381
-
382
-Add this to your build file's list of dependencies:
383
-
384
-```groovy
385
-    compile("org.springframework.boot:spring-boot-starter-actuator:0.5.0.M4")
386
-```
387
-
388
-If you are using Maven, add this to your list of dependencies:
389
-
390
-```xml
391
-        <dependency>
392
-            <groupId>org.springframework.boot</groupId>
393
-            <artifactId>spring-boot-starter-actuator</artifactId>
394
-        </dependency>
395
-```
396
-
397
-Then restart the app:
398
-
399
-```sh
400
-$ ./gradlew build && java -jar build/libs/gs-spring-boot-0.1.0.jar
401
-```
402
-
403
-If you are using Maven, execute:
404
-
405
-```sh
406
-$ mvn package && java -jar target/gs-spring-boot-0.1.0.jar
407
-```
408
-
409
-You will see a new set of RESTful end points added to the application. These are management services provided by Spring Boot.
410
-
411
-```
412
-2013-08-01 08:03:42.592  INFO 43851 ... Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.Map<java.lang.String, java.lang.Object> org.springframework.boot.ops.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
413
-2013-08-01 08:03:42.592  INFO 43851 ... Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.ops.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
414
-2013-08-01 08:03:42.844  INFO 43851 ... Mapped URL path [/env] onto handler of type [class org.springframework.boot.ops.endpoint.EnvironmentEndpoint]
415
-2013-08-01 08:03:42.844  INFO 43851 ... Mapped URL path [/health] onto handler of type [class org.springframework.boot.ops.endpoint.HealthEndpoint]
416
-2013-08-01 08:03:42.844  INFO 43851 ... Mapped URL path [/beans] onto handler of type [class org.springframework.boot.ops.endpoint.BeansEndpoint]
417
-2013-08-01 08:03:42.844  INFO 43851 ... Mapped URL path [/info] onto handler of type [class org.springframework.boot.ops.endpoint.InfoEndpoint]
418
-2013-08-01 08:03:42.845  INFO 43851 ... Mapped URL path [/metrics] onto handler of type [class org.springframework.boot.ops.endpoint.MetricsEndpoint]
419
-2013-08-01 08:03:42.845  INFO 43851 ... Mapped URL path [/trace] onto handler of type [class org.springframework.boot.ops.endpoint.TraceEndpoint]
420
-2013-08-01 08:03:42.845  INFO 43851 ... Mapped URL path [/dump] onto handler of type [class org.springframework.boot.ops.endpoint.DumpEndpoint]
421
-2013-08-01 08:03:42.845  INFO 43851 ... Mapped URL path [/shutdown] onto handler of type [class org.springframework.boot.ops.endpoint.ShutdownEndpoint]
422
-```
423
-
424
-They include: errors, [environment](http://localhost:8080/env), [health](http://localhost:8080/health), [beans](http://localhost:8080/beans), [info](http://localhost:8080/info), [metrics](http://localhost:8080/metrics), [trace](http://localhost:8080/trace), [dump](http://localhost:8080/dump), and shutdown.
425
-
426
-It's easy to check the health of the app.
427
-
428
-```sh
429
-$ curl localhost:8080/health
430
-ok
431
-```
432
-
433
-You can invoke shutdown through curl.
434
-
435
-```sh
436
-$ curl -X POST localhost:8080/shutdown
437
-```
438
-
439
-The response shows that shutdown through REST is currently disabled by default:
440
-```sh
441
-{"message":"Shutdown not enabled, sorry."}
442
-```
443
-
444
-Whew! You probably don't want that until you are ready to turn on proper security settings, if at all.
445
-
446
-For more details about each of these REST points and how you can tune their settings with an `application.properties` file, check out the [Spring Boot][spring-boot] project.
447
-
448
-View Spring Boot's starters
449
-----------------------
450
-You have seen some of Spring Boot's **starters**. Here is a complete list:
451
-- spring-boot-starter-actuator
452
-- spring-boot-starter-batch
453
-- spring-boot-starter-data-jpa
454
-- spring-boot-starter-integration
455
-- spring-boot-starter-jetty
456
-- spring-boot-starter-logging
457
-- spring-boot-starter-parent
458
-- spring-boot-starter-security
459
-- spring-boot-starter-tomcat
460
-- spring-boot-starter-web
461
-
462
-
463
-JAR support and Groovy support
464
-------------------------------
465
-The last example showed how Spring Boot makes it easy to wire beans you may not be aware that you need. And it showed how to turn on convenient management services.
466
-
467
-But Spring Boot does yet more. It supports not only traditional WAR file deployments, but also makes it easy to put together executable JARs thanks to Spring Boot's loader module. The various guides demonstrate this dual support through the `spring-boot-gradle-plugin` and `spring-boot-maven-plugin`.
468
-
469
-On top of that, Spring Boot also has Groovy support, allowing you to build Spring MVC web apps with as little as a single file.
470
-
471
-Create a new file called **app.groovy** and put the following code in it:
472
-
473
-```groovy
474
-@RestController
475
-class ThisWillActuallyRun {
476
-
477
-    @RequestMapping("/")
478
-    String home() {
479
-        return "Hello World!"
480
-    }
481
-
482
-}
483
-```
484
-
485
-> **Note:** It doesn't matter where the file is. You can even fit an application that small inside a [single tweet](https://twitter.com/rob_winch/status/364871658483351552)!
486
-
487
-Next, [install Spring Boot's CLI](https://github.com/spring-projects/spring-boot#installing-the-cli).
488
-
489
-Run it as follows:
490
-
491
-```sh
492
-$ spring run app.groovy
493
-```
494
-
495
-> **Note:** This assumes you shut down the previous application, to avoid a port collision.
496
-
497
-From a different terminal window:
498
-```sh
499
-$ curl localhost:8080
500
-Hello World!
501
-```
502
-
503
-Spring Boot does this by dynamically adding key annotations to your code and leveraging [Groovy Grapes](http://groovy.codehaus.org/Grape) to pull down needed libraries to make the app run.
504
-
505
-Summary
506
-----------------
507
-Congratulations! You built a simple web application with Spring Boot and learned how it can ramp up your development pace. You also turned on some handy production services.
508
-
509
-[spring-boot]: https://github.com/spring-projects/spring-boot
510
-[spring-boot-actuator]: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-actuator/README.md
511
-[gs-uploading-files]: /guides/gs/uploading-files