|
|
@@ -64,11 +64,11 @@ The service will handle `GET` requests for `/greeting`, optionally with a `name`
|
|
64
|
64
|
|
|
65
|
65
|
The `id` field is a unique identifier for the greeting, and `content` is the textual representation of the greeting.
|
|
66
|
66
|
|
|
67
|
|
-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:
|
|
|
67
|
+To model the greeting representation, you create a resource representation class. Provide a plain old java object with fields, constructors, and accessors for the `id` and `content` data:
|
|
68
|
68
|
|
|
69
|
69
|
<@snippet path="src/main/java/hello/Greeting.java" prefix="complete"/>
|
|
70
|
70
|
|
|
71
|
|
-> **Note:** As you'll see in steps below, Spring will use the _Jackson_ JSON library to automatically marshal instances of type `Greeting` into JSON.
|
|
|
71
|
+> **Note:** As you see in steps below, Spring uses the [Jackson JSON][jackson] library to automatically marshal instances of type `Greeting` into JSON.
|
|
72
|
72
|
|
|
73
|
73
|
Next you create the resource controller that will serve these greetings.
|
|
74
|
74
|
|
|
|
@@ -76,7 +76,7 @@ Next you create the resource controller that will serve these greetings.
|
|
76
|
76
|
Create a resource controller
|
|
77
|
77
|
------------------------------
|
|
78
|
78
|
|
|
79
|
|
-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:
|
|
|
79
|
+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:
|
|
80
|
80
|
|
|
81
|
81
|
<@snippet path="src/main/java/hello/GreetingController.java" prefix="complete"/>
|
|
82
|
82
|
|
|
|
@@ -84,7 +84,7 @@ This controller is concise and simple, but there's plenty going on under the hoo
|
|
84
|
84
|
|
|
85
|
85
|
The `@RequestMapping` annotation ensures that HTTP requests to `/greeting` are mapped to the `greeting()` method.
|
|
86
|
86
|
|
|
87
|
|
-> **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.
|
|
|
87
|
+> **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.
|
|
88
|
88
|
|
|
89
|
89
|
`@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.
|
|
90
|
90
|
|
|
|
@@ -94,19 +94,19 @@ A key difference between a traditional MVC controller and the RESTful web servic
|
|
94
|
94
|
|
|
95
|
95
|
To accomplish 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.
|
|
96
|
96
|
|
|
97
|
|
-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.
|
|
|
97
|
+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.
|
|
98
|
98
|
|
|
99
|
99
|
|
|
100
|
100
|
Make the application executable
|
|
101
|
101
|
-------------------------------
|
|
102
|
102
|
|
|
103
|
|
-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's support for embedding the [Tomcat][u-tomcat] servlet container as the HTTP runtime, instead of deploying to an external instance.
|
|
|
103
|
+Although it is possible to package this service as a traditional [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. Along the way, you use Spring's support for embedding the [Tomcat][u-tomcat] servlet container as the HTTP runtime, instead of deploying to an external instance.
|
|
104
|
104
|
|
|
105
|
105
|
### Create a main class
|
|
106
|
106
|
|
|
107
|
107
|
<@snippet path="src/main/java/hello/Application.java" prefix="complete"/>
|
|
108
|
108
|
|
|
109
|
|
-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]_.
|
|
|
109
|
+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].
|
|
110
|
110
|
|
|
111
|
111
|
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.
|
|
112
|
112
|
|
|
|
@@ -144,7 +144,9 @@ Notice also how the `id` attribute has changed from `1` to `2`. This proves that
|
|
144
|
144
|
Summary
|
|
145
|
145
|
-------
|
|
146
|
146
|
|
|
147
|
|
-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 take advantage of. Be sure to check out Spring's support for [securing](TODO), [describing](TODO) [managing](TODO), [testing](TODO) and [consuming](/gs-consuming-rest) RESTful web services.
|
|
|
147
|
+Congratulations! You've just developed a RESTful web service with Spring.
|
|
|
148
|
+
|
|
|
149
|
+To see what else you can do with Spring and RESTful web services, see [Related Resources](TODO).
|
|
148
|
150
|
|
|
149
|
151
|
|
|
150
|
152
|
[zip]: https://github.com/springframework-meta/gs-rest-service/archive/master.zip
|