Kr Younger пре 6 година
родитељ
комит
34d28ea495
1 измењених фајлова са 53 додато и 61 уклоњено
  1. 53
    61
      README.md

README.adoc → README.md Прегледај датотеку

@@ -1,20 +1,13 @@
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 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 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 3
 http://start.spring.io/[Spring Initializr], fill in your project details, pick your options, and you can download either
10 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 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 11
 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[]
19 12
 
20 13
 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/how_to_complete_this_guide.adoc[]
@@ -26,7 +19,7 @@ include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/
26 19
 
27 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 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,44 +33,43 @@ These are just a few examples of the automatic configuration Spring Boot provide
40 33
 
41 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 37
 Now you can create a web controller for a simple web application.
45 38
 
46 39
 `src/main/java/hello/HelloController.java`
47
-[source,java]
48
-----
40
+```
49 41
 include::initial/src/main/java/hello/HelloController.java[]
50
-----
42
+```
51 43
 
52 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 47
 Here you create an `Application` class with the components:
56 48
 
57 49
 `src/main/java/hello/Application.java`
58
-[source,java]
59
-----
50
+
51
+```
60 52
 include::complete/src/main/java/hello/Application.java[]
61
-----
53
+```
62 54
 
63 55
 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/spring-boot-application.adoc[]
64 56
 
65 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 60
 To run the application, execute:
69 61
 
70
-[subs="attributes"]
71
-----
62
+
63
+```
72 64
 ./gradlew build && java -jar build/libs/{project_id}-0.1.0.jar
73
-----
65
+```
74 66
 
75 67
 If you are using Maven, execute:
76 68
 
77
-[subs="attributes"]
78
-----
69
+
70
+```
79 71
 mvn package && java -jar target/{project_id}-0.1.0.jar
80
-----
72
+```
81 73
 
82 74
 You should see some output like this:
83 75
 
@@ -128,74 +120,74 @@ $ curl localhost:8080
128 120
 Greetings from Spring Boot!
129 121
 ....
130 122
 
131
-== Add Unit Tests
123
+## Add Unit Tests
132 124
 
133 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 127
 Add this to your build file's list of dependencies:
136 128
 
137 129
 [source,groovy]
138
-----
130
+```
139 131
 include::complete/build.gradle[tag=tests]
140
-----
132
+```
141 133
 
142 134
 If you are using Maven, add this to your list of dependencies:
143 135
 
144 136
 [source,xml]
145
-----
137
+```
146 138
 include::complete/pom.xml[tag=tests]
147
-----
139
+```
148 140
 
149 141
 Now write a simple unit test that mocks the servlet request and response through your endpoint:
150 142
 
151 143
 `src/test/java/hello/HelloControllerTest.java`
152
-[source,java]
153
-----
144
+
145
+```
154 146
 include::complete/src/test/java/hello/HelloControllerTest.java[]
155
-----
147
+```
156 148
 
157 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 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 153
 `src/test/java/hello/HelloControllerIT.java`
162
-[source,java]
163
-----
154
+
155
+```
164 156
 include::complete/src/test/java/hello/HelloControllerIT.java[]
165
-----
157
+```
166 158
 
167 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 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 164
 Add this to your build file's list of dependencies:
173 165
 
174 166
 [source,groovy]
175
-----
167
+```
176 168
 include::complete/build.gradle[tag=actuator]
177
-----
169
+```
178 170
 
179 171
 If you are using Maven, add this to your list of dependencies:
180 172
 
181 173
 [source,xml]
182
-----
174
+```
183 175
 include::complete/pom.xml[tag=actuator]
184
-----
176
+```
185 177
 
186 178
 Then restart the app:
187 179
 
188
-[subs="attributes"]
189
-----
180
+
181
+```
190 182
 ./gradlew build && java -jar build/libs/{project_id}-0.1.0.jar
191
-----
183
+```
192 184
 
193 185
 If you are using Maven, execute:
194 186
 
195
-[subs="attributes"]
196
-----
187
+
188
+```
197 189
 mvn package && java -jar target/{project_id}-0.1.0.jar
198
-----
190
+```
199 191
 
200 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,26 +209,26 @@ NOTE: There is also a `/actuator/shutdown` endpoint, but it's only visible by de
217 209
 
218 210
 It's easy to check the health of the app.
219 211
 
220
-----
212
+```
221 213
 $ curl localhost:8080/actuator/health
222 214
 {"status":"UP"}
223
-----
215
+```
224 216
 
225 217
 You can try to invoke shutdown through curl.
226 218
 
227
-----
219
+```
228 220
 $ curl -X POST localhost:8080/actuator/shutdown
229 221
 {"timestamp":1401820343710,"error":"Method Not Allowed","status":405,"message":"Request method 'POST' not supported"}
230
-----
222
+```
231 223
 
232 224
 Because we didn't enable it, the request is blocked by the virtue of not existing.
233 225
 
234 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 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 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 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,7 +238,7 @@ On top of that, Spring Boot also has Groovy support, allowing you to build Sprin
246 238
 Create a new file called **app.groovy** and put the following code in it:
247 239
 
248 240
 [source,groovy]
249
-----
241
+```
250 242
 @RestController
251 243
 class ThisWillActuallyRun {
252 244
 
@@ -256,7 +248,7 @@ class ThisWillActuallyRun {
256 248
     }
257 249
 
258 250
 }
259
-----
251
+```
260 252
 
261 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,26 +256,26 @@ Next, http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htm
264 256
 
265 257
 Run it as follows:
266 258
 
267
-----
259
+```
268 260
 $ spring run app.groovy
269
-----
261
+```
270 262
 
271 263
 NOTE: This assumes you shut down the previous application, to avoid a port collision.
272 264
 
273 265
 From a different terminal window:
274
-----
266
+```
275 267
 $ curl localhost:8080
276 268
 Hello World!
277
-----
269
+```
278 270
 
279 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 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 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 276
 if you want to dig deeper.
285 277
 
286
-== See Also
278
+## See Also
287 279
 
288 280
 The following guides may also be helpful:
289 281