|
|
@@ -2,28 +2,49 @@
|
|
2
|
2
|
|
|
3
|
3
|
Introduction
|
|
4
|
4
|
------------
|
|
5
|
|
-This Getting Started guide will walk you through the process of creating a simple REST service using Spring.
|
|
6
|
5
|
|
|
7
|
|
-To help you get started, we've provided an initial project structure as well as the completed project for you in GitHub:
|
|
|
6
|
+### What You'll Build
|
|
8
|
7
|
|
|
9
|
|
-```sh
|
|
10
|
|
-$ git clone https://github.com/springframework-meta/gs-rest-service.git
|
|
|
8
|
+This guide will take you through creating a "hello world" [RESTful web service](/understanding/REST) with Spring—literally, we'll build a service that accepts an HTTP GET request:
|
|
11
|
9
|
```
|
|
|
10
|
+$ curl http://localhost:8080/hello-world
|
|
|
11
|
+```
|
|
|
12
|
+and responds with the following [JSON](/understanding/JSON):
|
|
|
13
|
+```
|
|
|
14
|
+{"id":1,"content":"Hello, World!"}
|
|
|
15
|
+```
|
|
|
16
|
+
|
|
|
17
|
+### What You'll Need
|
|
|
18
|
+
|
|
|
19
|
+ - About 15 minutes
|
|
|
20
|
+ - A favorite text editor or IDE
|
|
|
21
|
+ - [JDK 7](http://docs.oracle.com/javase/7/docs/webnotes/install/index.html) or better
|
|
|
22
|
+ - Your choice of Maven (3.0+) or Gradle (1.5+)
|
|
12
|
23
|
|
|
13
|
|
-In the `start` folder, you'll find a bare project, ready for you to copy-n-paste code snippets from this document. In the `complete` folder, you'll find the complete project code.
|
|
|
24
|
+### How to Complete this Guide
|
|
14
|
25
|
|
|
15
|
|
-Before we can write the REST service itself, there's some initial project setup that's required. Or, you can skip straight to the [fun part](#creating-a-representation-class).
|
|
|
26
|
+Like all Spring's [Getting Started guides](/getting-started), you can choose to start from scratch and complete each step, or you can jump past basic setup steps that may already be familiar to you. Either way, you'll end up with working code.
|
|
16
|
27
|
|
|
|
28
|
+To **start from scratch**, just move on to the next section and start [setting up the project](#scratch).
|
|
17
|
29
|
|
|
18
|
|
-Adding dependencies
|
|
19
|
|
--------------------
|
|
20
|
|
-First you'll need to set up a basic build script. You can use any build system you like, but we've included snippets for [Maven](https://maven.apache.org) and [Gradle](http://gradle.org) here. If you're not familiar with either of these, you can refer to our [Getting Started with Maven](../gs-maven/README.md) or [Getting Started with Gradle](../gs-gradle/README.md) guides.
|
|
|
30
|
+If you'd like to **skip the basics**, then do the following:
|
|
21
|
31
|
|
|
22
|
|
-Add the [Spring MVC](TODO) and [Jackson](http://jackson.codehaus.org) JSON libraries as dependencies:
|
|
|
32
|
+ - [download][zip] and unzip the source repository for this guide—or clone it using [git](/understanding/git):
|
|
|
33
|
+`git clone https://github.com/springframework-meta/gs-rest-service.git`
|
|
|
34
|
+ - cd into `gs-rest-service/initial`
|
|
|
35
|
+ - jump ahead to [creating a representation class](#initial).
|
|
|
36
|
+
|
|
|
37
|
+And **when you're finished**, you can check your results against the the code in `gs-rest-service/complete`.
|
|
|
38
|
+
|
|
|
39
|
+
|
|
|
40
|
+<a name="scratch"></a>
|
|
|
41
|
+Setting up the project
|
|
|
42
|
+----------------------
|
|
|
43
|
+First you'll need to set up a basic build script. You can use any build system you like when building apps with Spring, but we've included what you'll need to work with [Maven](https://maven.apache.org) and [Gradle](http://gradle.org) here. If you're not familiar with either of these, you can refer to our [Getting Started with Maven](../gs-maven/README.md) or [Getting Started with Gradle](../gs-gradle/README.md) guides.
|
|
23
|
44
|
|
|
24
|
45
|
### Maven
|
|
25
|
46
|
|
|
26
|
|
-Create a `pom.xml` file with the following contents:
|
|
|
47
|
+Create a Maven POM that looks like this:
|
|
27
|
48
|
|
|
28
|
49
|
`pom.xml`
|
|
29
|
50
|
```xml
|
|
|
@@ -33,13 +54,13 @@ Create a `pom.xml` file with the following contents:
|
|
33
|
54
|
<modelVersion>4.0.0</modelVersion>
|
|
34
|
55
|
|
|
35
|
56
|
<groupId>org.springframework</groupId>
|
|
36
|
|
- <artifactId>gs-rest-service-complete</artifactId>
|
|
37
|
|
- <version>0.0.1-SNAPSHOT</version>
|
|
|
57
|
+ <artifactId>gs-rest-service</artifactId>
|
|
|
58
|
+ <version>1.0-SNAPSHOT</version>
|
|
38
|
59
|
|
|
39
|
60
|
<parent>
|
|
40
|
61
|
<groupId>org.springframework.bootstrap</groupId>
|
|
41
|
62
|
<artifactId>spring-bootstrap-starters</artifactId>
|
|
42
|
|
- <version>0.0.1-SNAPSHOT</version>
|
|
|
63
|
+ <version>0.5.0.BUILD-SNAPSHOT</version>
|
|
43
|
64
|
</parent>
|
|
44
|
65
|
|
|
45
|
66
|
<dependencies>
|
|
|
@@ -47,18 +68,28 @@ Create a `pom.xml` file with the following contents:
|
|
47
|
68
|
<groupId>org.springframework.bootstrap</groupId>
|
|
48
|
69
|
<artifactId>spring-bootstrap-web-starter</artifactId>
|
|
49
|
70
|
</dependency>
|
|
50
|
|
- <dependency>
|
|
51
|
|
- <groupId>com.fasterxml.jackson.core</groupId>
|
|
52
|
|
- <artifactId>jackson-databind</artifactId>
|
|
53
|
|
- </dependency>
|
|
54
|
71
|
</dependencies>
|
|
55
|
72
|
|
|
|
73
|
+ <!-- TODO: remove once bootstrap goes GA -->
|
|
|
74
|
+ <repositories>
|
|
|
75
|
+ <repository>
|
|
|
76
|
+ <id>spring-snapshots</id>
|
|
|
77
|
+ <name>Spring Snapshots</name>
|
|
|
78
|
+ <url>http://repo.springsource.org/snapshot</url>
|
|
|
79
|
+ <snapshots><enabled>true</enabled></snapshots>
|
|
|
80
|
+ </repository>
|
|
|
81
|
+ </repositories>
|
|
|
82
|
+
|
|
56
|
83
|
</project>
|
|
57
|
84
|
```
|
|
58
|
85
|
|
|
|
86
|
+TODO: mention that we're using Spring Bootstrap's [_starter POMs_](../gs-bootstrap-starter) here.
|
|
|
87
|
+
|
|
59
|
88
|
Experienced Maven users who feel nervous about using an external parent project: don't panic, you can take it out later, it's just there to reduce the amount of code you have to write to get started.
|
|
60
|
89
|
|
|
61
|
|
-### Gradle \[[copy complete `build.gradle` to clipboard](start/build.gradle)\]
|
|
|
90
|
+### Gradle
|
|
|
91
|
+
|
|
|
92
|
+TODO: paste complete build.gradle.
|
|
62
|
93
|
|
|
63
|
94
|
Add the following within the `dependencies { }` section of your build.gradle file:
|
|
64
|
95
|
|
|
|
@@ -92,6 +123,8 @@ public class HelloWorldConfiguration {
|
|
92
|
123
|
This class is concise, but there's plenty going on under the hood. [`@EnableWebMvc`](http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/config/annotation/EnableWebMvc.html) handles the registration of a number of components that enable Spring's support for annotation-based controllers—you'll build one of those in an upcoming step. And we've also annotated the configuration class with [`@ComponentScan`](http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/context/annotation/ComponentScan.html) which tells Spring to scan the `hello` package for those controllers (along with any other annotated component classes).
|
|
93
|
124
|
|
|
94
|
125
|
|
|
|
126
|
+<a name="initial"></a>
|
|
|
127
|
+
|
|
95
|
128
|
Creating a Representation Class
|
|
96
|
129
|
-------------------------------
|
|
97
|
130
|
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.
|
|
|
@@ -103,7 +136,7 @@ What we want is to handle GET requests for /hello-world, optionally with a name
|
|
103
|
136
|
```json
|
|
104
|
137
|
{
|
|
105
|
138
|
"id": 1,
|
|
106
|
|
- "content": "Hello, stranger!"
|
|
|
139
|
+ "content": "Hello, World!"
|
|
107
|
140
|
}
|
|
108
|
141
|
```
|
|
109
|
142
|
|
|
|
@@ -201,32 +234,6 @@ public class HelloWorldConfiguration {
|
|
201
|
234
|
|
|
202
|
235
|
The `@EnableAutoConfiguration` annotation has also been added: it provides a load of defaults (like the embedded servlet container) depending on the contents of your classpath, and other things.
|
|
203
|
236
|
|
|
204
|
|
-Running the Service
|
|
205
|
|
--------------------------------------
|
|
206
|
|
-
|
|
207
|
|
-Add the following to your `pom.xml`:
|
|
208
|
|
-
|
|
209
|
|
-`pom.xml`
|
|
210
|
|
-```xml
|
|
211
|
|
-<properties>
|
|
212
|
|
- <start-class>hello.HelloWorldConfiguration</start-class>
|
|
213
|
|
-</properties>
|
|
214
|
|
-```
|
|
215
|
|
-
|
|
216
|
|
-You can now run the application with the Maven exec plugin:
|
|
217
|
|
-
|
|
218
|
|
-```
|
|
219
|
|
-$ mvn exec:java
|
|
220
|
|
-
|
|
221
|
|
-... service comes up ...
|
|
222
|
|
-```
|
|
223
|
|
-
|
|
224
|
|
-so in another terminal you can do this
|
|
225
|
|
-
|
|
226
|
|
-```
|
|
227
|
|
-$ curl localhost:8080/hello-world
|
|
228
|
|
-{"id":1,"content":"Hello, Stranger!"}
|
|
229
|
|
-```
|
|
230
|
237
|
|
|
231
|
238
|
Building an executable JAR
|
|
232
|
239
|
--------------------------
|
|
|
@@ -251,6 +258,9 @@ The following will produce a single executable JAR file containing all necessary
|
|
251
|
258
|
$ mvn package
|
|
252
|
259
|
```
|
|
253
|
260
|
|
|
|
261
|
+Running the Service
|
|
|
262
|
+-------------------------------------
|
|
|
263
|
+
|
|
254
|
264
|
Now you can run it from the jar as well, and distribute that as an executable artifact:
|
|
255
|
265
|
|
|
256
|
266
|
```
|
|
|
@@ -259,13 +269,13 @@ $ java -jar target/gs-rest-service-0.0.1-SNAPSHOT.jar
|
|
259
|
269
|
... service comes up ...
|
|
260
|
270
|
```
|
|
261
|
271
|
|
|
262
|
|
-Congratulations! You have just developed a simple REST service using Spring. This is a basic foundation for building a complete REST API in Spring.
|
|
|
272
|
+Congratulations! You have just developed a simple RESTful service using Spring. This is a basic foundation for building a complete REST API in Spring.
|
|
263
|
273
|
|
|
264
|
274
|
|
|
265
|
275
|
Related Resources
|
|
266
|
276
|
-----------------
|
|
267
|
277
|
|
|
268
|
|
-There's more to building REST services than is covered here. You may want to continue your exploration of Spring and REST with the following Getting Started guides:
|
|
|
278
|
+There's more to building RESTful web services than is covered here. You may want to continue your exploration of Spring and REST with the following Getting Started guides:
|
|
269
|
279
|
|
|
270
|
280
|
* Handling POST, PUT, and GET requests in REST services
|
|
271
|
281
|
* Creating self-describing APIs with HATEOAS
|
|
|
@@ -275,3 +285,4 @@ There's more to building REST services than is covered here. You may want to con
|
|
275
|
285
|
* Testing REST services
|
|
276
|
286
|
|
|
277
|
287
|
|
|
|
288
|
+[zip]: https://github.com/springframework-meta/gs-rest-service/archive/master.zip
|