|
|
@@ -57,7 +57,7 @@ Create a Maven POM that looks like this:
|
|
57
|
57
|
|
|
58
|
58
|
<groupId>org.springframework</groupId>
|
|
59
|
59
|
<artifactId>gs-rest-service</artifactId>
|
|
60
|
|
- <version>0.0.1-SNAPSHOT</version>
|
|
|
60
|
+ <version>1.0</version>
|
|
61
|
61
|
|
|
62
|
62
|
<parent>
|
|
63
|
63
|
<groupId>org.springframework.bootstrap</groupId>
|
|
|
@@ -149,7 +149,7 @@ What we want is to handle GET requests for /hello-world, optionally with a name
|
|
149
|
149
|
"content": "Hello, World!"
|
|
150
|
150
|
}
|
|
151
|
151
|
```
|
|
152
|
|
-
|
|
|
152
|
+
|
|
153
|
153
|
The `id` field is a unique identifier for the greeting, and `content` is the textual representation of the greeting.
|
|
154
|
154
|
|
|
155
|
155
|
To model the greeting representation, we’ll create a representation class:
|
|
|
@@ -164,16 +164,16 @@ public class Greeting {
|
|
164
|
164
|
private final String content;
|
|
165
|
165
|
|
|
166
|
166
|
public Greeting(long id, String content) {
|
|
167
|
|
- this.id = id;
|
|
168
|
|
- this.content = content;
|
|
|
167
|
+ this.id = id;
|
|
|
168
|
+ this.content = content;
|
|
169
|
169
|
}
|
|
170
|
170
|
|
|
171
|
171
|
public long getId() {
|
|
172
|
|
- return id;
|
|
|
172
|
+ return id;
|
|
173
|
173
|
}
|
|
174
|
174
|
|
|
175
|
175
|
public String getContent() {
|
|
176
|
|
- return content;
|
|
|
176
|
+ return content;
|
|
177
|
177
|
}
|
|
178
|
178
|
}
|
|
179
|
179
|
```
|
|
|
@@ -209,9 +209,9 @@ public class HelloWorldController {
|
|
209
|
209
|
}
|
|
210
|
210
|
```
|
|
211
|
211
|
|
|
212
|
|
-The key difference between a human-facing controller and a REST endpoint controller is in how the response is created. Rather than rely on a view (such as JSP) to render model data in HTML, an endpoint controller simply returns the data to be written directly to the body of the response.
|
|
|
212
|
+The key difference between a human-facing controller and a REST endpoint controller is in how the response is created. Rather than rely on a view (such as a [JSP](/understanding/JSP)) to render model data in HTML, an endpoint controller simply returns the data to be written directly to the body of the response.
|
|
213
|
213
|
|
|
214
|
|
-The magic is in the [`@ResponseBody`](http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/bind/annotation/ResponseBody.html) annotation. `@ResponseBody` tells Spring MVC to not render a model into a view, but rather to write the returned object into the response body. It does this by using one of Spring's message converters. Because Jackson 2 is in the classpath, this means that [`MappingJackson2HttpMessageConverter`](http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.html) will handle the conversion of Greeting to JSON if the request's `Accept` header specifies that JSON should be returned.
|
|
|
214
|
+The magic is in the [`@ResponseBody`](http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/bind/annotation/ResponseBody.html) annotation. `@ResponseBody` tells Spring MVC to not render a model into a view, but rather to write the returned object into the response body. It does this by using one of Spring's message converters. Because Jackson 2 is on the classpath, Spring's [`MappingJackson2HttpMessageConverter`](http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.html) is automatically chosen to convert the Greeting instance to JSON.
|
|
215
|
215
|
|
|
216
|
216
|
|
|
217
|
217
|
Creating an executable main class
|
|
|
@@ -251,6 +251,10 @@ Add the following to your `pom.xml` file (keeping any existing properties or plu
|
|
251
|
251
|
|
|
252
|
252
|
`pom.xml`
|
|
253
|
253
|
```xml
|
|
|
254
|
+<properties>
|
|
|
255
|
+ <start-class>hello.HelloWorldConfiguration</start-class>
|
|
|
256
|
+</properties>
|
|
|
257
|
+
|
|
254
|
258
|
<build>
|
|
255
|
259
|
<plugins>
|
|
256
|
260
|
<plugin>
|
|
|
@@ -271,7 +275,7 @@ Running the Service
|
|
271
|
275
|
|
|
272
|
276
|
Now you can run it from the jar as well, and distribute that as an executable artifact:
|
|
273
|
277
|
```
|
|
274
|
|
-$ java -jar target/gs-rest-service-1.0-SNAPSHOT.jar
|
|
|
278
|
+$ java -jar target/gs-rest-service-1.0.jar
|
|
275
|
279
|
|
|
276
|
280
|
... service comes up ...
|
|
277
|
281
|
```
|