|
|
@@ -5,7 +5,7 @@
|
|
5
|
5
|
What you'll build
|
|
6
|
6
|
-----------------
|
|
7
|
7
|
|
|
8
|
|
-This guide will walk you through creating a "hello world" [RESTful web service][u-rest] with Spring. The service will accept HTTP GET requests at:
|
|
|
8
|
+This guide walks you through creating a "hello world" [RESTful web service][u-rest] with Spring. The service will accept HTTP GET requests at:
|
|
9
|
9
|
|
|
10
|
10
|
http://localhost:8080/greeting
|
|
11
|
11
|
|
|
|
@@ -13,11 +13,11 @@ and respond with a [JSON][u-json] representation of a greeting:
|
|
13
|
13
|
|
|
14
|
14
|
{"id":1,"content":"Hello, World!"}
|
|
15
|
15
|
|
|
16
|
|
-You'll also be able to customize the greeting by providing an optional `name` parameter in the query string:
|
|
|
16
|
+You can customize the greeting with an optional `name` parameter in the query string:
|
|
17
|
17
|
|
|
18
|
18
|
http://localhost:8080/greeting?name=User
|
|
19
|
19
|
|
|
20
|
|
-this will override the default value of "World" and be reflected in the response:
|
|
|
20
|
+The `name` parameter value overrides the default value of "World" and is reflected in the response:
|
|
21
|
21
|
|
|
22
|
22
|
{"id":1,"content":"Hello, User!"}
|
|
23
|
23
|
|
|
|
@@ -41,7 +41,7 @@ Set up the project
|
|
41
|
41
|
|
|
42
|
42
|
### Create the directory structure
|
|
43
|
43
|
|
|
44
|
|
-In a project directory of your choosing, create the following subdirectory structure (e.g. with `mkdir -p src/main/java/hello` on *nix systems):
|
|
|
44
|
+In a project directory of your choosing, create the following subdirectory structure; for example, with `mkdir -p src/main/java/hello` on *nix systems:
|
|
45
|
45
|
|
|
46
|
46
|
└── src
|
|
47
|
47
|
└── main
|
|
|
@@ -105,11 +105,11 @@ In a project directory of your choosing, create the following subdirectory struc
|
|
105
|
105
|
Create a resource representation class
|
|
106
|
106
|
--------------------------------------
|
|
107
|
107
|
|
|
108
|
|
-With the basics of setting up the project and build system out of the way, it's time to get to the nuts and bolts of creating our service.
|
|
|
108
|
+Now that you've set up the project and build system, you can create your web service.
|
|
109
|
109
|
|
|
110
|
|
-It's best to begin this process by thinking about what interacting with the service will look like.
|
|
|
110
|
+Begin the process by thinking about service interactions.
|
|
111
|
111
|
|
|
112
|
|
-We want to handle `GET` requests for `/greeting`, optionally with a `name` parameter in the query string. In response to such a request, we'd like to send back a `200 OK` response with JSON in the body that represents a greeting. It should look something like this:
|
|
|
112
|
+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:
|
|
113
|
113
|
|
|
114
|
114
|
{
|
|
115
|
115
|
"id": 1,
|
|
|
@@ -118,7 +118,7 @@ We want to handle `GET` requests for `/greeting`, optionally with a `name` param
|
|
118
|
118
|
|
|
119
|
119
|
The `id` field is a unique identifier for the greeting, and `content` is the textual representation of the greeting.
|
|
120
|
120
|
|
|
121
|
|
-To model the greeting representation, we'll create a _resource representation class_. There's nothing fancy about this step—we're just creating a plain old java object with fields, constructors and accessors for the `id` and `content` data:
|
|
|
121
|
+To model the greeting representation, you create a _resource representation class_. To do this, you simply create a plain old java object with fields, constructors, and accessors for the `id` and `content` data:
|
|
122
|
122
|
|
|
123
|
123
|
`src/main/java/hello/Greeting.java`
|
|
124
|
124
|
```java
|
|
|
@@ -144,15 +144,15 @@ public class Greeting {
|
|
144
|
144
|
}
|
|
145
|
145
|
```
|
|
146
|
146
|
|
|
147
|
|
-As you'll see in a moment, Spring will the use _Jackson_ library to automatically marshal instances of type `Greeting` into JSON.
|
|
|
147
|
+As you'll see, Spring uses _Jackson_ library to automatically marshal instances of type `Greeting` into JSON.
|
|
148
|
148
|
|
|
149
|
|
-Now that we've got our representation class, let's create the resource controller that will serve it.
|
|
|
149
|
+Next you create the resource controller that will serve the resource representation class.
|
|
150
|
150
|
|
|
151
|
151
|
|
|
152
|
152
|
Create a resource controller
|
|
153
|
153
|
------------------------------
|
|
154
|
154
|
|
|
155
|
|
-In Spring's approach to building RESTful web services, HTTP requests are handled by a _controller_. These components are are easily identified by the [`@Controller`][] annotation, and the `GreetingController` below handles `GET` requests for `/greeting` by returning a new instance of our `Greeting` class:
|
|
|
155
|
+In Spring's approach to building RESTful web services, HTTP requests are handled by a _controller_. These components are easily identified by the [`@Controller`][] annotation, and the `GreetingController` below handles `GET` requests for `/greeting` by returning a new instance of the `Greeting` class:
|
|
156
|
156
|
|
|
157
|
157
|
`src/main/java/hello/GreetingController.java`
|
|
158
|
158
|
```java
|
|
|
@@ -183,23 +183,23 @@ This controller is concise and simple, but there's plenty going on under the hoo
|
|
183
|
183
|
|
|
184
|
184
|
The `@RequestMapping` annotation ensures that HTTP requests to `/greeting` are mapped to the `greeting()` method.
|
|
185
|
185
|
|
|
186
|
|
-> **Note:** We have not explicitly specified `GET` vs. `PUT`, `POST`, etc. above, because `@RequestMapping` maps _all_ HTTP operations by default. Use `@RequestMapping(method=GET)` to narrow this down.
|
|
|
186
|
+> **Note:** The above example does not specify `GET` vs. `PUT`, `POST`, and so forth, because `@RequestMapping` maps _all_ HTTP operations by default. Use `@RequestMapping(method=GET)` to narrow this mapping.
|
|
187
|
187
|
|
|
188
|
|
-`@RequestParam` binds the value of the query string parameter `name` into the `name` parameter of the `greeting()` method. This query string parameter is not `required`, so if absent in the request the `defaultValue` of "World" will be used.
|
|
|
188
|
+`@RequestParam` binds the value of the query string parameter `name` into the `name` parameter of the `greeting()` method. This query string parameter is not `required`; if it is absent in the request, the `defaultValue` of "World" is used.
|
|
189
|
189
|
|
|
190
|
|
-The implementation of the method body is straightforward—create and return a new `Greeting` object with `id` and `content` attributes based on the next value from our `counter` and formatting the given `name` using our greeting `template`.
|
|
|
190
|
+The implementation of the method body creates and returns a new `Greeting` object with `id` and `content` attributes based on the next value from the `counter`, and formats the given `name` by using the greeting `template`.
|
|
191
|
191
|
|
|
192
|
|
-One key difference between a traditional MVC controller and the RESTful web service controller above is in the way the HTTP response body is created. Rather than relying on a view technology (such as [JSP][u-jsp]) to perform server-side rendering of our greeting data to HTML, this service controller simply populates and returns a `Greeting` object, with the goal that its data is written directly to the HTTP response as JSON.
|
|
|
192
|
+A key difference between a traditional MVC controller and the RESTful web service controller above is the way that the HTTP response body is created. Rather than relying on a view technology (such as [JSP][u-jsp]) to perform server-side rendering of the greeting data to HTML, this RESTful web service controller simply populates and returns a `Greeting` object. The object data is written directly to the HTTP response as JSON.
|
|
193
|
193
|
|
|
194
|
|
-The [`@ResponseBody`][] annotation helps make this happen. The presence of this annotation on the `greeting()` method tells Spring MVC that it does not need to render the greeting object through a server-side view layer, but that instead that the greeting object returned _is_ the response body, and should be written out directly.
|
|
|
194
|
+To accmplish this, the [`@ResponseBody`][] annotation on the `greeting()` method tells Spring MVC that it does not need to render the greeting object through a server-side view layer, but that instead that the greeting object returned _is_ the response body, and should be written out directly.
|
|
195
|
195
|
|
|
196
|
|
-The only step that remains is converting the `Greeting` object to JSON. And fortunately, thanks to Spring's _HTTP message converter_ support, we don't need to bother with doing this conversion by hand. Because [Jackson 2][jackson] is on the classpath, Spring's [`MappingJackson2HttpMessageConverter`][] is automatically chosen to convert the `Greeting` instance to JSON.
|
|
|
196
|
+The `Greeting` object must be converted to JSON. Thanks to Spring's _HTTP message converter_ support, you don't need to do this conversion manually. Because [Jackson 2][jackson] is on the classpath, Spring's [`MappingJackson2HttpMessageConverter`][] is automatically chosen to convert the `Greeting` instance to JSON.
|
|
197
|
197
|
|
|
198
|
198
|
|
|
199
|
199
|
Make the application executable
|
|
200
|
200
|
-------------------------------
|
|
201
|
201
|
|
|
202
|
|
-While it is possible to package this service up as a traditional _web application archive_ or [WAR][u-war] file for deployment to an external application server, we demonstrate below the simpler approach of creating a _standalone application_. You'll package everything up a single, executable JAR file, driven by a good old Java `main()` method. And along the way, we'll use Spring's support for embedding the [Tomcat][u-tomcat] servlet container as the HTTP runtime, instead of deploying to an external instance.
|
|
|
202
|
+Although it is possible to package this service as a traditional _web application archive_ or [WAR][u-war] file for deployment to an external application server, the simpler approach demonstrated below creates a _standalone application_. You package everything in a single, executable JAR file, driven by a good old Java `main()` method. And along the way, you use Spring support for embedding the [Tomcat][u-tomcat] servlet container as the HTTP runtime, instead of deploying to an external instance.
|
|
203
|
203
|
|
|
204
|
204
|
### Create a main class
|
|
205
|
205
|
|
|
|
@@ -223,15 +223,15 @@ public class Application {
|
|
223
|
223
|
```
|
|
224
|
224
|
The `main()` method defers to the [`SpringApplication`][] helper class, providing `Application.class` as an argument to its `run()` method. This tells Spring to read the annotation metadata from `Application` and to manage it as a component in the _[Spring application context][u-application-context]_.
|
|
225
|
225
|
|
|
226
|
|
-The `@ComponentScan` annotation tells Spring to recursively search through the `hello` package and its children for classes marked directly or indirectly with Spring's [`@Component`][] annotation. This directive ensures that Spring will find and register our `GreetingController`, because it is marked with `@Controller`, which in turn is a kind of `@Component` annotation.
|
|
|
226
|
+The `@ComponentScan` annotation tells Spring to search recursively through the `hello` package and its children for classes marked directly or indirectly with Spring's [`@Component`][] annotation. This directive ensures that Spring finds and registers the `GreetingController`, because it is marked with `@Controller`, which in turn is a kind of `@Component` annotation.
|
|
227
|
227
|
|
|
228
|
|
-The [`@EnableAutoConfiguration`][] annotation has also been added: it switches on a number of reasonable default behaviors based on the content of your classpath. For example, because the application depends on the embeddable version of Tomcat (tomcat-embed-core.jar), a Tomcat server is set up and configured with reasonable defaults on your behalf. And because the application also depends on Spring MVC (spring-webmvc.jar), a Spring MVC [`DispatcherServlet`][] is configured and registered for you—no `web.xml` necessary! Auto-configuration is a powerful, flexible mechanism—See the [API documentation][`@EnableAutoConfiguration`] for further details.
|
|
|
228
|
+The [`@EnableAutoConfiguration`][] annotation switches on reasonable default behaviors based on the content of your classpath. For example, because the application depends on the embeddable version of Tomcat (tomcat-embed-core.jar), a Tomcat server is set up and configured with reasonable defaults on your behalf. And because the application also depends on Spring MVC (spring-webmvc.jar), a Spring MVC [`DispatcherServlet`][] is configured and registered for you — no `web.xml` necessary! Auto-configuration is a powerful, flexible mechanism. See the [API documentation][`@EnableAutoConfiguration`] for further details.
|
|
229
|
229
|
|
|
230
|
230
|
### Build an executable JAR
|
|
231
|
231
|
|
|
232
|
|
-Now that we have our `Application` class ready to go, we simply need to instruct the build system to create a single, executable jar containing everything. This will make it dead simple to ship and version and deploy the service as an application throughout the development lifecycle, across different environments, etc.
|
|
|
232
|
+Now that your `Application` class is ready, you simply instruct the build system to create a single, executable jar containing everything. This makes it easy to ship, version, and deploy the service as an application throughout the development lifecycle, across different environments, and so forth.
|
|
233
|
233
|
|
|
234
|
|
-Add the following configuration to your existing Maven POM
|
|
|
234
|
+Add the following configuration to your existing Maven POM:
|
|
235
|
235
|
|
|
236
|
236
|
`pom.xml`
|
|
237
|
237
|
```xml
|
|
|
@@ -249,9 +249,9 @@ Add the following configuration to your existing Maven POM
|
|
249
|
249
|
</build>
|
|
250
|
250
|
```
|
|
251
|
251
|
|
|
252
|
|
-The `start-class` property tells Maven to create a `META-INF/MANIFEST.MF` file with a `Main-Class: hello.Application` entry. This is the key to being able to run the jar with `java -jar`.
|
|
|
252
|
+The `start-class` property tells Maven to create a `META-INF/MANIFEST.MF` file with a `Main-Class: hello.Application` entry. This entry enables you to run the jar with `java -jar`.
|
|
253
|
253
|
|
|
254
|
|
-The [Maven Shade plugin][maven-shade-plugin] extracts classes from all the jars on the classpath and builds a single "über-jar". This makes it much more convenient to execute and transport your service.
|
|
|
254
|
+The [Maven Shade plugin][maven-shade-plugin] extracts classes from all the jars on the classpath and builds a single "über-jar", which makes it more convenient to execute and transport your service.
|
|
255
|
255
|
|
|
256
|
256
|
Now run the following to produce a single executable JAR file containing all necessary dependency classes and resources:
|
|
257
|
257
|
|
|
|
@@ -261,33 +261,33 @@ Now run the following to produce a single executable JAR file containing all nec
|
|
261
|
261
|
Run the service
|
|
262
|
262
|
---------------
|
|
263
|
263
|
|
|
264
|
|
-That's it! You're ready to run your service with `java -jar` at the command line:
|
|
|
264
|
+Run your service with `java -jar` at the command line:
|
|
265
|
265
|
|
|
266
|
266
|
java -jar target/gs-rest-service-1.0.jar
|
|
267
|
267
|
|
|
268
|
|
-You'll see a bit of logging output, and the service should be up and running within a second or two.
|
|
|
268
|
+Logging output is displayed. The service should be up and running within a few seconds.
|
|
269
|
269
|
|
|
270
|
270
|
|
|
271
|
271
|
Test the service
|
|
272
|
272
|
----------------
|
|
273
|
273
|
|
|
274
|
|
-Now that the service is up, visit <http://localhost:8080/greeting>, where you'll see
|
|
|
274
|
+Now that the service is up, visit <http://localhost:8080/greeting>, where you see:
|
|
275
|
275
|
|
|
276
|
276
|
{"id":1,"content":"Hello, World!"}
|
|
277
|
277
|
|
|
278
|
|
-Try providing 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!":
|
|
|
278
|
+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!":
|
|
279
|
279
|
|
|
280
|
280
|
{"id":2,"content":"Hello, User!"}
|
|
281
|
281
|
|
|
282
|
|
-This 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.
|
|
|
282
|
+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.
|
|
283
|
283
|
|
|
284
|
|
-Notice also how the `id` attribute has changed from `1` to `2`. This proves that we're working against the same `GreetingController` instance across multiple requests, and that its `counter` field is being incremented on each call as expected.
|
|
|
284
|
+Notice also how the `id` attribute has changed from `1` to `2`. This proves that you are working against the same `GreetingController` instance across multiple requests, and that its `counter` field is being incremented on each call as expected.
|
|
285
|
285
|
|
|
286
|
286
|
|
|
287
|
287
|
Summary
|
|
288
|
288
|
-------
|
|
289
|
289
|
|
|
290
|
|
-Congrats! You've just developed your first RESTful web service using Spring. This of course is just the beginning, and there are many more features to explore and take advantage of. Be sure to check out Spring's support for [securing](TODO), [testing](TODO), [describing](TODO) and [managing](TODO) RESTful web services.
|
|
|
290
|
+Congrats! You've just developed a RESTful web service with Spring. This of course is just the beginning, and there are many more features to explore and implement. Be sure to check out Spring's support for [securing](TODO), [testing](TODO), [describing](TODO) and [managing](TODO) RESTful web services.
|
|
291
|
291
|
|
|
292
|
292
|
|
|
293
|
293
|
[mvn]: http://maven.apache.org/download.cgi
|