|
|
@@ -50,52 +50,45 @@ Create a Maven POM that looks like this:
|
|
50
|
50
|
```xml
|
|
51
|
51
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
52
|
52
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
53
|
|
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
54
|
|
- <modelVersion>4.0.0</modelVersion>
|
|
55
|
|
-
|
|
56
|
|
- <groupId>org.springframework</groupId>
|
|
57
|
|
- <artifactId>gs-rest-service</artifactId>
|
|
58
|
|
- <version>0.0.1-SNAPSHOT</version>
|
|
59
|
|
-
|
|
60
|
|
- <parent>
|
|
61
|
|
- <groupId>org.springframework.bootstrap</groupId>
|
|
62
|
|
- <artifactId>spring-bootstrap-starters</artifactId>
|
|
63
|
|
- <version>0.5.0.BUILD-SNAPSHOT</version>
|
|
64
|
|
- </parent>
|
|
65
|
|
-
|
|
66
|
|
- <dependencies>
|
|
67
|
|
- <dependency>
|
|
68
|
|
- <groupId>org.springframework.bootstrap</groupId>
|
|
69
|
|
- <artifactId>spring-bootstrap-web-starter</artifactId>
|
|
70
|
|
- </dependency>
|
|
71
|
|
- <dependency>
|
|
72
|
|
- <groupId>com.fasterxml.jackson.core</groupId>
|
|
73
|
|
- <artifactId>jackson-databind</artifactId>
|
|
74
|
|
- </dependency>
|
|
75
|
|
- </dependencies>
|
|
76
|
|
-
|
|
77
|
|
- <!-- TODO: remove once bootstrap goes GA -->
|
|
78
|
|
- <repositories>
|
|
79
|
|
- <repository>
|
|
80
|
|
- <id>spring-snapshots</id>
|
|
81
|
|
- <name>Spring Snapshots</name>
|
|
82
|
|
- <url>http://repo.springsource.org/snapshot</url>
|
|
83
|
|
- <snapshots>
|
|
84
|
|
- <enabled>true</enabled>
|
|
85
|
|
- </snapshots>
|
|
86
|
|
- </repository>
|
|
87
|
|
- </repositories>
|
|
88
|
|
- <pluginRepositories>
|
|
89
|
|
- <pluginRepository>
|
|
90
|
|
- <id>spring-snapshots</id>
|
|
91
|
|
- <name>Spring Snapshots</name>
|
|
92
|
|
- <url>http://repo.springsource.org/snapshot</url>
|
|
93
|
|
- <snapshots>
|
|
94
|
|
- <enabled>true</enabled>
|
|
95
|
|
- </snapshots>
|
|
96
|
|
- </pluginRepository>
|
|
97
|
|
- </pluginRepositories>
|
|
98
|
|
-
|
|
|
53
|
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
|
54
|
+ <modelVersion>4.0.0</modelVersion>
|
|
|
55
|
+
|
|
|
56
|
+ <groupId>org.springframework</groupId>
|
|
|
57
|
+ <artifactId>gs-rest-service</artifactId>
|
|
|
58
|
+ <version>0.0.1-SNAPSHOT</version>
|
|
|
59
|
+
|
|
|
60
|
+ <parent>
|
|
|
61
|
+ <groupId>org.springframework.bootstrap</groupId>
|
|
|
62
|
+ <artifactId>spring-bootstrap-starters</artifactId>
|
|
|
63
|
+ <version>0.5.0.BUILD-SNAPSHOT</version>
|
|
|
64
|
+ </parent>
|
|
|
65
|
+
|
|
|
66
|
+ <dependencies>
|
|
|
67
|
+ <dependency>
|
|
|
68
|
+ <groupId>org.springframework.bootstrap</groupId>
|
|
|
69
|
+ <artifactId>spring-bootstrap-web-starter</artifactId>
|
|
|
70
|
+ </dependency>
|
|
|
71
|
+ <dependency>
|
|
|
72
|
+ <groupId>com.fasterxml.jackson.core</groupId>
|
|
|
73
|
+ <artifactId>jackson-databind</artifactId>
|
|
|
74
|
+ </dependency>
|
|
|
75
|
+ </dependencies>
|
|
|
76
|
+
|
|
|
77
|
+ <!-- TODO: remove once bootstrap goes GA -->
|
|
|
78
|
+ <repositories>
|
|
|
79
|
+ <repository>
|
|
|
80
|
+ <id>spring-snapshots</id>
|
|
|
81
|
+ <url>http://repo.springsource.org/snapshot</url>
|
|
|
82
|
+ <snapshots><enabled>true</enabled></snapshots>
|
|
|
83
|
+ </repository>
|
|
|
84
|
+ </repositories>
|
|
|
85
|
+ <pluginRepositories>
|
|
|
86
|
+ <pluginRepository>
|
|
|
87
|
+ <id>spring-snapshots</id>
|
|
|
88
|
+ <url>http://repo.springsource.org/snapshot</url>
|
|
|
89
|
+ <snapshots><enabled>true</enabled></snapshots>
|
|
|
90
|
+ </pluginRepository>
|
|
|
91
|
+ </pluginRepositories>
|
|
99
|
92
|
</project>
|
|
100
|
93
|
```
|
|
101
|
94
|
|
|
|
@@ -140,7 +133,6 @@ This class is concise, but there's plenty going on under the hood. [`@EnableWebM
|
|
140
|
133
|
|
|
141
|
134
|
|
|
142
|
135
|
<a name="initial"></a>
|
|
143
|
|
-
|
|
144
|
136
|
Creating a Representation Class
|
|
145
|
137
|
-------------------------------
|
|
146
|
138
|
With the essential Spring MVC configuration out of the way, it's time to get to the nuts and bolts of our REST service by creating a resource representation class and an endpoint controller.
|
|
|
@@ -181,7 +173,6 @@ public class Greeting {
|
|
181
|
173
|
public String getContent() {
|
|
182
|
174
|
return content;
|
|
183
|
175
|
}
|
|
184
|
|
-
|
|
185
|
176
|
}
|
|
186
|
177
|
```
|
|
187
|
178
|
|
|
|
@@ -204,15 +195,15 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|
204
|
195
|
@Controller
|
|
205
|
196
|
@RequestMapping("/hello-world")
|
|
206
|
197
|
public class HelloWorldController {
|
|
207
|
|
-
|
|
|
198
|
+
|
|
208
|
199
|
private static final String template = "Hello, %s!";
|
|
209
|
200
|
private final AtomicLong counter = new AtomicLong();
|
|
210
|
201
|
|
|
211
|
202
|
@RequestMapping(method=RequestMethod.GET)
|
|
212
|
|
- public @ResponseBody Greeting sayHello(@RequestParam(value="name", required=false, defaultValue="Stranger") String name) {
|
|
|
203
|
+ public @ResponseBody Greeting sayHello(
|
|
|
204
|
+ @RequestParam(value="name", required=false, defaultValue="Stranger") String name) {
|
|
213
|
205
|
return new Greeting(counter.incrementAndGet(), String.format(template, name));
|
|
214
|
206
|
}
|
|
215
|
|
-
|
|
216
|
207
|
}
|
|
217
|
208
|
```
|
|
218
|
209
|
|
|
|
@@ -258,13 +249,6 @@ Add the following to your `pom.xml` file (keeping any existing properties or plu
|
|
258
|
249
|
|
|
259
|
250
|
`pom.xml`
|
|
260
|
251
|
```xml
|
|
261
|
|
-<properties>
|
|
262
|
|
- <!-- use UTF-8 for everything -->
|
|
263
|
|
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
264
|
|
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
|
265
|
|
- <start-class>hello.HelloWorldConfiguration</start-class>
|
|
266
|
|
-</properties>
|
|
267
|
|
-
|
|
268
|
252
|
<build>
|
|
269
|
253
|
<plugins>
|
|
270
|
254
|
<plugin>
|
|
|
@@ -276,7 +260,6 @@ Add the following to your `pom.xml` file (keeping any existing properties or plu
|
|
276
|
260
|
```
|
|
277
|
261
|
|
|
278
|
262
|
The following will produce a single executable JAR file containing all necessary dependency classes:
|
|
279
|
|
-
|
|
280
|
263
|
```
|
|
281
|
264
|
$ mvn package
|
|
282
|
265
|
```
|
|
|
@@ -285,9 +268,8 @@ Running the Service
|
|
285
|
268
|
-------------------------------------
|
|
286
|
269
|
|
|
287
|
270
|
Now you can run it from the jar as well, and distribute that as an executable artifact:
|
|
288
|
|
-
|
|
289
|
271
|
```
|
|
290
|
|
-$ java -jar target/gs-rest-service-0.0.1-SNAPSHOT.jar
|
|
|
272
|
+$ java -jar target/gs-rest-service-1.0-SNAPSHOT.jar
|
|
291
|
273
|
|
|
292
|
274
|
... service comes up ...
|
|
293
|
275
|
```
|
|
|
@@ -307,5 +289,4 @@ There's more to building RESTful web services than is covered here. You may want
|
|
307
|
289
|
* [Consuming REST services](https://github.com/springframework-meta/gs-consuming-rest-core/blob/master/README.md)
|
|
308
|
290
|
* Testing REST services
|
|
309
|
291
|
|
|
310
|
|
-
|
|
311
|
292
|
[zip]: https://github.com/springframework-meta/gs-rest-service/archive/master.zip
|