Kr Younger 6 years ago
parent
commit
34d28ea495
1 changed files with 53 additions and 61 deletions
  1. 53
    61
      README.md

README.adoc → README.md View File

1
-:spring_boot_version: 2.0.3.RELEASE
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.
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.
8
 It is meant to give you a quick taste of Spring Boot. If you want to create your own Spring Boot-based project, visit
2
 It is meant to give you a quick taste of Spring Boot. If you want to create your own Spring Boot-based project, visit
9
 http://start.spring.io/[Spring Initializr], fill in your project details, pick your options, and you can download either
3
 http://start.spring.io/[Spring Initializr], fill in your project details, pick your options, and you can download either
10
 a Maven build file, or a bundled up project as a zip file.
4
 a Maven build file, or a bundled up project as a zip file.
11
 
5
 
12
-== What you'll build
6
+## What you'll build
13
 You'll build a simple web application with Spring Boot and add some useful services to it.
7
 You'll build a simple web application with Spring Boot and add some useful services to it.
14
 
8
 
15
-== What you'll need
9
+## What you'll need
16
 
10
 
17
-:java_version: 1.8
18
 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[]
11
 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[]
19
 
12
 
20
 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/how_to_complete_this_guide.adoc[]
13
 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/how_to_complete_this_guide.adoc[]
26
 
19
 
27
 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-sts.adoc[]
20
 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-sts.adoc[]
28
 
21
 
29
-== Learn what you can do with Spring Boot
22
+## Learn what you can do with Spring Boot
30
 
23
 
31
 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.
24
 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
 
25
 
40
 
33
 
41
 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.
34
 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.
42
 
35
 
43
-== Create a simple web application
36
+## Create a simple web application
44
 Now you can create a web controller for a simple web application.
37
 Now you can create a web controller for a simple web application.
45
 
38
 
46
 `src/main/java/hello/HelloController.java`
39
 `src/main/java/hello/HelloController.java`
47
-[source,java]
48
-----
40
+```
49
 include::initial/src/main/java/hello/HelloController.java[]
41
 include::initial/src/main/java/hello/HelloController.java[]
50
-----
42
+```
51
 
43
 
52
 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.
44
 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.
53
 
45
 
54
-== Create an Application class
46
+## Create an Application class
55
 Here you create an `Application` class with the components:
47
 Here you create an `Application` class with the components:
56
 
48
 
57
 `src/main/java/hello/Application.java`
49
 `src/main/java/hello/Application.java`
58
-[source,java]
59
-----
50
+
51
+```
60
 include::complete/src/main/java/hello/Application.java[]
52
 include::complete/src/main/java/hello/Application.java[]
61
-----
53
+```
62
 
54
 
63
 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/spring-boot-application.adoc[]
55
 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/spring-boot-application.adoc[]
64
 
56
 
65
 There is also a `CommandLineRunner` method marked as a `@Bean` and this runs on start up. It 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.
57
 There is also a `CommandLineRunner` method marked as a `@Bean` and this runs on start up. It 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.
66
 
58
 
67
-== Run the application
59
+## Run the application
68
 To run the application, execute:
60
 To run the application, execute:
69
 
61
 
70
-[subs="attributes"]
71
-----
62
+
63
+```
72
 ./gradlew build && java -jar build/libs/{project_id}-0.1.0.jar
64
 ./gradlew build && java -jar build/libs/{project_id}-0.1.0.jar
73
-----
65
+```
74
 
66
 
75
 If you are using Maven, execute:
67
 If you are using Maven, execute:
76
 
68
 
77
-[subs="attributes"]
78
-----
69
+
70
+```
79
 mvn package && java -jar target/{project_id}-0.1.0.jar
71
 mvn package && java -jar target/{project_id}-0.1.0.jar
80
-----
72
+```
81
 
73
 
82
 You should see some output like this:
74
 You should see some output like this:
83
 
75
 
128
 Greetings from Spring Boot!
120
 Greetings from Spring Boot!
129
 ....
121
 ....
130
 
122
 
131
-== Add Unit Tests
123
+## Add Unit Tests
132
 
124
 
133
 You will want to add a test for the endpoint you added, and Spring Test already provides some machinery for that, and it's easy to include in your project.
125
 You will want to add a test for the endpoint you added, and Spring Test already provides some machinery for that, and it's easy to include in your project.
134
 
126
 
135
 Add this to your build file's list of dependencies:
127
 Add this to your build file's list of dependencies:
136
 
128
 
137
 [source,groovy]
129
 [source,groovy]
138
-----
130
+```
139
 include::complete/build.gradle[tag=tests]
131
 include::complete/build.gradle[tag=tests]
140
-----
132
+```
141
 
133
 
142
 If you are using Maven, add this to your list of dependencies:
134
 If you are using Maven, add this to your list of dependencies:
143
 
135
 
144
 [source,xml]
136
 [source,xml]
145
-----
137
+```
146
 include::complete/pom.xml[tag=tests]
138
 include::complete/pom.xml[tag=tests]
147
-----
139
+```
148
 
140
 
149
 Now write a simple unit test that mocks the servlet request and response through your endpoint:
141
 Now write a simple unit test that mocks the servlet request and response through your endpoint:
150
 
142
 
151
 `src/test/java/hello/HelloControllerTest.java`
143
 `src/test/java/hello/HelloControllerTest.java`
152
-[source,java]
153
-----
144
+
145
+```
154
 include::complete/src/test/java/hello/HelloControllerTest.java[]
146
 include::complete/src/test/java/hello/HelloControllerTest.java[]
155
-----
147
+```
156
 
148
 
157
 The `MockMvc` comes from Spring Test and allows you, via a set of convenient builder classes, to send HTTP requests into the `DispatcherServlet` and make assertions about the result. Note the use of the `@AutoConfigureMockMvc` together with `@SpringBootTest` to inject a `MockMvc` instance. Having used `@SpringBootTest` we are asking for the whole application context to be created.  An alternative would be to ask Spring Boot to create only the web layers of the context using the `@WebMvcTest`. Spring Boot automatically tries to locate the main application class of your application in either case, but you can override it, or narrow it down, if you want to build something different.
149
 The `MockMvc` comes from Spring Test and allows you, via a set of convenient builder classes, to send HTTP requests into the `DispatcherServlet` and make assertions about the result. Note the use of the `@AutoConfigureMockMvc` together with `@SpringBootTest` to inject a `MockMvc` instance. Having used `@SpringBootTest` we are asking for the whole application context to be created.  An alternative would be to ask Spring Boot to create only the web layers of the context using the `@WebMvcTest`. Spring Boot automatically tries to locate the main application class of your application in either case, but you can override it, or narrow it down, if you want to build something different.
158
 
150
 
159
 As well as mocking the HTTP request cycle we can also use Spring Boot to write a very simple full-stack integration test. For example, instead of (or as well as) the mock test above we could do this:
151
 As well as mocking the HTTP request cycle we can also use Spring Boot to write a very simple full-stack integration test. For example, instead of (or as well as) the mock test above we could do this:
160
 
152
 
161
 `src/test/java/hello/HelloControllerIT.java`
153
 `src/test/java/hello/HelloControllerIT.java`
162
-[source,java]
163
-----
154
+
155
+```
164
 include::complete/src/test/java/hello/HelloControllerIT.java[]
156
 include::complete/src/test/java/hello/HelloControllerIT.java[]
165
-----
157
+```
166
 
158
 
167
 The embedded server is started up on a random port by virtue of the `webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT` and the actual port is discovered at runtime with the `@LocalServerPort`.
159
 The embedded server is started up on a random port by virtue of the `webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT` and the actual port is discovered at runtime with the `@LocalServerPort`.
168
 
160
 
169
-== Add production-grade services
161
+## Add production-grade services
170
 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 http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#production-ready[actuator module], such as health, audits, beans, and more.
162
 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 http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#production-ready[actuator module], such as health, audits, beans, and more.
171
 
163
 
172
 Add this to your build file's list of dependencies:
164
 Add this to your build file's list of dependencies:
173
 
165
 
174
 [source,groovy]
166
 [source,groovy]
175
-----
167
+```
176
 include::complete/build.gradle[tag=actuator]
168
 include::complete/build.gradle[tag=actuator]
177
-----
169
+```
178
 
170
 
179
 If you are using Maven, add this to your list of dependencies:
171
 If you are using Maven, add this to your list of dependencies:
180
 
172
 
181
 [source,xml]
173
 [source,xml]
182
-----
174
+```
183
 include::complete/pom.xml[tag=actuator]
175
 include::complete/pom.xml[tag=actuator]
184
-----
176
+```
185
 
177
 
186
 Then restart the app:
178
 Then restart the app:
187
 
179
 
188
-[subs="attributes"]
189
-----
180
+
181
+```
190
 ./gradlew build && java -jar build/libs/{project_id}-0.1.0.jar
182
 ./gradlew build && java -jar build/libs/{project_id}-0.1.0.jar
191
-----
183
+```
192
 
184
 
193
 If you are using Maven, execute:
185
 If you are using Maven, execute:
194
 
186
 
195
-[subs="attributes"]
196
-----
187
+
188
+```
197
 mvn package && java -jar target/{project_id}-0.1.0.jar
189
 mvn package && java -jar target/{project_id}-0.1.0.jar
198
-----
190
+```
199
 
191
 
200
 You will see a new set of RESTful end points added to the application. These are management services provided by Spring Boot.
192
 You will see a new set of RESTful end points added to the application. These are management services provided by Spring Boot.
201
 
193
 
217
 
209
 
218
 It's easy to check the health of the app.
210
 It's easy to check the health of the app.
219
 
211
 
220
-----
212
+```
221
 $ curl localhost:8080/actuator/health
213
 $ curl localhost:8080/actuator/health
222
 {"status":"UP"}
214
 {"status":"UP"}
223
-----
215
+```
224
 
216
 
225
 You can try to invoke shutdown through curl.
217
 You can try to invoke shutdown through curl.
226
 
218
 
227
-----
219
+```
228
 $ curl -X POST localhost:8080/actuator/shutdown
220
 $ curl -X POST localhost:8080/actuator/shutdown
229
 {"timestamp":1401820343710,"error":"Method Not Allowed","status":405,"message":"Request method 'POST' not supported"}
221
 {"timestamp":1401820343710,"error":"Method Not Allowed","status":405,"message":"Request method 'POST' not supported"}
230
-----
222
+```
231
 
223
 
232
 Because we didn't enable it, the request is blocked by the virtue of not existing.
224
 Because we didn't enable it, the request is blocked by the virtue of not existing.
233
 
225
 
234
 For more details about each of these REST points and how you can tune their settings with an `application.properties` file, you can read detailed http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#production-ready-endpoints[docs about the endpoints].
226
 For more details about each of these REST points and how you can tune their settings with an `application.properties` file, you can read detailed http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#production-ready-endpoints[docs about the endpoints].
235
 
227
 
236
-== View Spring Boot's starters
228
+## View Spring Boot's starters
237
 You have seen some of http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#using-boot-starter[Spring Boot's "starters"]. You can see them all https://github.com/spring-projects/spring-boot/tree/master/spring-boot-project/spring-boot-starters[here in source code].
229
 You have seen some of http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#using-boot-starter[Spring Boot's "starters"]. You can see them all https://github.com/spring-projects/spring-boot/tree/master/spring-boot-project/spring-boot-starters[here in source code].
238
 
230
 
239
-== JAR support and Groovy support
231
+## JAR support and Groovy support
240
 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.
232
 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.
241
 
233
 
242
 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`.
234
 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`.
246
 Create a new file called **app.groovy** and put the following code in it:
238
 Create a new file called **app.groovy** and put the following code in it:
247
 
239
 
248
 [source,groovy]
240
 [source,groovy]
249
-----
241
+```
250
 @RestController
242
 @RestController
251
 class ThisWillActuallyRun {
243
 class ThisWillActuallyRun {
252
 
244
 
256
     }
248
     }
257
 
249
 
258
 }
250
 }
259
-----
251
+```
260
 
252
 
261
 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]!
253
 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]!
262
 
254
 
264
 
256
 
265
 Run it as follows:
257
 Run it as follows:
266
 
258
 
267
-----
259
+```
268
 $ spring run app.groovy
260
 $ spring run app.groovy
269
-----
261
+```
270
 
262
 
271
 NOTE: This assumes you shut down the previous application, to avoid a port collision.
263
 NOTE: This assumes you shut down the previous application, to avoid a port collision.
272
 
264
 
273
 From a different terminal window:
265
 From a different terminal window:
274
-----
266
+```
275
 $ curl localhost:8080
267
 $ curl localhost:8080
276
 Hello World!
268
 Hello World!
277
-----
269
+```
278
 
270
 
279
 Spring Boot does this by dynamically adding key annotations to your code and using http://groovy.codehaus.org/Grape[Groovy Grape] to pull down libraries needed to make the app run.
271
 Spring Boot does this by dynamically adding key annotations to your code and using http://groovy.codehaus.org/Grape[Groovy Grape] to pull down libraries needed to make the app run.
280
 
272
 
281
-== Summary
273
+## Summary
282
 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.
274
 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.
283
 This is only a small sampling of what Spring Boot can do. Checkout http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle[Spring Boot's online docs]
275
 This is only a small sampling of what Spring Boot can do. Checkout http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle[Spring Boot's online docs]
284
 if you want to dig deeper.
276
 if you want to dig deeper.
285
 
277
 
286
-== See Also
278
+## See Also
287
 
279
 
288
 The following guides may also be helpful:
280
 The following guides may also be helpful:
289
 
281