|
|
@@ -10,7 +10,9 @@ This guide walks you through creating a "hello world" [RESTful web service][u-re
|
|
10
|
10
|
|
|
11
|
11
|
and respond with a [JSON][u-json] representation of a greeting:
|
|
12
|
12
|
|
|
13
|
|
- {"id":1,"content":"Hello, World!"}
|
|
|
13
|
+```json
|
|
|
14
|
+{"id":1,"content":"Hello, World!"}
|
|
|
15
|
+```
|
|
14
|
16
|
|
|
15
|
17
|
You can customize the greeting with an optional `name` parameter in the query string:
|
|
16
|
18
|
|
|
|
@@ -18,7 +20,9 @@ You can customize the greeting with an optional `name` parameter in the query st
|
|
18
|
20
|
|
|
19
|
21
|
The `name` parameter value overrides the default value of "World" and is reflected in the response:
|
|
20
|
22
|
|
|
21
|
|
- {"id":1,"content":"Hello, User!"}
|
|
|
23
|
+```json
|
|
|
24
|
+{"id":1,"content":"Hello, User!"}
|
|
|
25
|
+```
|
|
22
|
26
|
|
|
23
|
27
|
|
|
24
|
28
|
What you'll need
|
|
|
@@ -129,10 +133,12 @@ Begin the process by thinking about service interactions.
|
|
129
|
133
|
|
|
130
|
134
|
The service will handle `GET` requests for `/greeting`, optionally with a `name` parameter in the query string. The `GET` request should return a `200 OK` response with JSON in the body that represents a greeting. It should look something like this:
|
|
131
|
135
|
|
|
132
|
|
- {
|
|
133
|
|
- "id": 1,
|
|
134
|
|
- "content": "Hello, World!"
|
|
135
|
|
- }
|
|
|
136
|
+```json
|
|
|
137
|
+{
|
|
|
138
|
+ "id": 1,
|
|
|
139
|
+ "content": "Hello, World!"
|
|
|
140
|
+}
|
|
|
141
|
+```
|
|
136
|
142
|
|
|
137
|
143
|
The `id` field is a unique identifier for the greeting, and `content` is the textual representation of the greeting.
|
|
138
|
144
|
|
|
|
@@ -273,7 +279,9 @@ The [Spring Package maven plugin][spring-package-maven-plugin] collects all the
|
|
273
|
279
|
|
|
274
|
280
|
Now run the following to produce a single executable JAR file containing all necessary dependency classes and resources:
|
|
275
|
281
|
|
|
276
|
|
- mvn package
|
|
|
282
|
+```sh
|
|
|
283
|
+$ mvn package
|
|
|
284
|
+```
|
|
277
|
285
|
|
|
278
|
286
|
[spring-package-maven-plugin]: https://github.com/SpringSource/spring-zero/tree/master/spring-package-maven-plugin
|
|
279
|
287
|
|
|
|
@@ -283,8 +291,9 @@ Run the service
|
|
283
|
291
|
-------------------
|
|
284
|
292
|
Run your service with `java -jar` at the command line:
|
|
285
|
293
|
|
|
286
|
|
- java -jar target/gs-rest-service-0.1.0.jar
|
|
287
|
|
-
|
|
|
294
|
+```sh
|
|
|
295
|
+$ java -jar target/gs-rest-service-0.1.0.jar
|
|
|
296
|
+```
|
|
288
|
297
|
|
|
289
|
298
|
|
|
290
|
299
|
Logging output is displayed. The service should be up and running within a few seconds.
|
|
|
@@ -295,11 +304,15 @@ Test the service
|
|
295
|
304
|
|
|
296
|
305
|
Now that the service is up, visit <http://localhost:8080/greeting>, where you see:
|
|
297
|
306
|
|
|
298
|
|
- {"id":1,"content":"Hello, World!"}
|
|
|
307
|
+```json
|
|
|
308
|
+{"id":1,"content":"Hello, World!"}
|
|
|
309
|
+```
|
|
299
|
310
|
|
|
300
|
311
|
Provide a `name` query string parameter with <http://localhost:8080/greeting?name=User>. Notice how the value of the `content` attribute changes from "Hello, World!" to "Hello User!":
|
|
301
|
312
|
|
|
302
|
|
- {"id":2,"content":"Hello, User!"}
|
|
|
313
|
+```json
|
|
|
314
|
+{"id":2,"content":"Hello, User!"}
|
|
|
315
|
+```
|
|
303
|
316
|
|
|
304
|
317
|
This change demonstrates that the `@RequestParam` arrangement in `GreetingController` is working as expected. The `name` parameter has been given a default value of "World", but can always be explicitly overridden through the query string.
|
|
305
|
318
|
|