|
|
|
|
|
|
1
|
-# Getting Started Building a RESTful Web Service
|
|
|
|
|
|
1
|
+# Getting Started: Building a RESTful Web Service
|
|
2
|
|
2
|
|
|
3
|
[](https://drone.io/github.com/springframework-meta/gs-rest-service/latest)
|
3
|
[](https://drone.io/github.com/springframework-meta/gs-rest-service/latest)
|
|
4
|
|
4
|
|
|
5
|
-Introduction
|
|
|
|
6
|
-------------
|
|
|
|
7
|
|
5
|
|
|
8
|
-### What You'll Build
|
|
|
|
|
|
6
|
+What you'll build
|
|
|
|
7
|
+-----------------
|
|
9
|
|
8
|
|
|
10
|
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:
|
9
|
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
|
```
|
10
|
```
|
|
|
|
|
|
|
16
|
{"id":1,"content":"Hello, World!"}
|
15
|
{"id":1,"content":"Hello, World!"}
|
|
17
|
```
|
16
|
```
|
|
18
|
|
17
|
|
|
19
|
-### What You'll Need
|
|
|
|
|
|
18
|
+
|
|
|
|
19
|
+What you'll need
|
|
|
|
20
|
+----------------
|
|
20
|
|
21
|
|
|
21
|
- About 15 minutes
|
22
|
- About 15 minutes
|
|
22
|
- A favorite text editor or IDE
|
23
|
- A favorite text editor or IDE
|
|
23
|
- - [JDK 7](http://docs.oracle.com/javase/7/docs/webnotes/install/index.html) or better
|
|
|
|
|
|
24
|
+ - [JDK 7][jdk7] or better
|
|
24
|
- Your choice of Maven (3.0+) or Gradle (1.5+)
|
25
|
- Your choice of Maven (3.0+) or Gradle (1.5+)
|
|
25
|
|
26
|
|
|
26
|
-### How to Complete this Guide
|
|
|
|
|
|
27
|
+
|
|
|
|
28
|
+How to complete this guide
|
|
|
|
29
|
+--------------------------
|
|
27
|
|
30
|
|
|
28
|
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.
|
31
|
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.
|
|
29
|
|
32
|
|
|
|
|
|
|
|
34
|
- [download][zip] and unzip the source repository for this guide—or clone it using [git](/understanding/git):
|
37
|
- [download][zip] and unzip the source repository for this guide—or clone it using [git](/understanding/git):
|
|
35
|
`git clone https://github.com/springframework-meta/gs-rest-service.git`
|
38
|
`git clone https://github.com/springframework-meta/gs-rest-service.git`
|
|
36
|
- cd into `gs-rest-service/initial`
|
39
|
- cd into `gs-rest-service/initial`
|
|
37
|
- - jump ahead to [creating a representation class](#initial).
|
|
|
|
|
|
40
|
+ - jump ahead to [create a representation class](#initial).
|
|
38
|
|
41
|
|
|
39
|
And **when you're finished**, you can check your results against the the code in `gs-rest-service/complete`.
|
42
|
And **when you're finished**, you can check your results against the the code in `gs-rest-service/complete`.
|
|
40
|
|
43
|
|
|
41
|
|
44
|
|
|
42
|
<a name="scratch"></a>
|
45
|
<a name="scratch"></a>
|
|
43
|
-Setting up the project
|
|
|
|
44
|
-----------------------
|
|
|
|
|
|
46
|
+Set up the project
|
|
|
|
47
|
+------------------
|
|
45
|
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.
|
48
|
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.
|
|
46
|
|
49
|
|
|
47
|
-### Maven
|
|
|
|
48
|
-
|
|
|
|
|
|
50
|
+<span class="maven">
|
|
49
|
Create a Maven POM that looks like this:
|
51
|
Create a Maven POM that looks like this:
|
|
50
|
|
52
|
|
|
51
|
`pom.xml`
|
53
|
`pom.xml`
|
|
|
|
|
|
|
97
|
TODO: mention that we're using Spring Bootstrap's [_starter POMs_](../gs-bootstrap-starter) here.
|
99
|
TODO: mention that we're using Spring Bootstrap's [_starter POMs_](../gs-bootstrap-starter) here.
|
|
98
|
|
100
|
|
|
99
|
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.
|
101
|
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.
|
|
|
|
102
|
+</span>
|
|
100
|
|
103
|
|
|
101
|
-### Gradle
|
|
|
|
102
|
-
|
|
|
|
103
|
-TODO: paste complete build.gradle.
|
|
|
|
104
|
-
|
|
|
|
105
|
-Add the following within the `dependencies { }` section of your build.gradle file:
|
|
|
|
106
|
-
|
|
|
|
|
|
104
|
+<span class="gradle">
|
|
107
|
`build.gradle`
|
105
|
`build.gradle`
|
|
108
|
```groovy
|
106
|
```groovy
|
|
|
|
107
|
+TODO: paste complete build.gradle.
|
|
109
|
compile "org.springframework.bootstrap:spring-bootstrap-web-starter:0.0.1-SNAPSHOT"
|
108
|
compile "org.springframework.bootstrap:spring-bootstrap-web-starter:0.0.1-SNAPSHOT"
|
|
110
|
compile "com.fasterxml.jackson.core:jackson-databind:2.2.0-"
|
109
|
compile "com.fasterxml.jackson.core:jackson-databind:2.2.0-"
|
|
111
|
```
|
110
|
```
|
|
|
|
111
|
+</span>
|
|
112
|
|
112
|
|
|
113
|
|
113
|
|
|
114
|
-Creating a Configuration Class
|
|
|
|
115
|
-------------------------------
|
|
|
|
|
|
114
|
+Create a configuration class
|
|
|
|
115
|
+----------------------------
|
|
116
|
The first step is to set up a simple Spring configuration class. It'll look like this:
|
116
|
The first step is to set up a simple Spring configuration class. It'll look like this:
|
|
117
|
|
117
|
|
|
118
|
`src/main/java/hello/HelloWorldConfiguration.java`
|
118
|
`src/main/java/hello/HelloWorldConfiguration.java`
|
|
|
|
|
|
|
135
|
|
135
|
|
|
136
|
|
136
|
|
|
137
|
<a name="initial"></a>
|
137
|
<a name="initial"></a>
|
|
138
|
-Creating a Representation Class
|
|
|
|
139
|
--------------------------------
|
|
|
|
|
|
138
|
+Create a representation class
|
|
|
|
139
|
+-----------------------------
|
|
140
|
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.
|
140
|
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.
|
|
141
|
|
141
|
|
|
142
|
Before we get too carried away with building the endpoint controller, we need to give some thought to what our API will look like.
|
142
|
Before we get too carried away with building the endpoint controller, we need to give some thought to what our API will look like.
|
|
|
|
|
|
|
180
|
|
180
|
|
|
181
|
Now that we've got our representation class, let's create the endpoint controller that will serve it.
|
181
|
Now that we've got our representation class, let's create the endpoint controller that will serve it.
|
|
182
|
|
182
|
|
|
183
|
-Creating a Resource Controller
|
|
|
|
|
|
183
|
+
|
|
|
|
184
|
+Create a resource controller
|
|
184
|
------------------------------
|
185
|
------------------------------
|
|
185
|
In Spring, REST endpoints are just Spring MVC controllers. The following Spring MVC controller handles a GET request for /hello-world and returns our `Greeting` resource:
|
186
|
In Spring, REST endpoints are just Spring MVC controllers. The following Spring MVC controller handles a GET request for /hello-world and returns our `Greeting` resource:
|
|
186
|
|
187
|
|
|
|
|
|
|
|
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
|
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
|
|
216
|
|
|
216
|
|
217
|
|
|
217
|
-Creating an executable main class
|
|
|
|
|
|
218
|
+Create an executable main class
|
|
218
|
---------------------------------
|
219
|
---------------------------------
|
|
219
|
|
220
|
|
|
220
|
We can launch the application from a custom main class, or we can do that directly from one of the configuration classes. The easiest way is to use the `SpringApplication` helper class:
|
221
|
We can launch the application from a custom main class, or we can do that directly from one of the configuration classes. The easiest way is to use the `SpringApplication` helper class:
|
|
|
|
|
|
|
244
|
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.
|
245
|
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.
|
|
245
|
|
246
|
|
|
246
|
|
247
|
|
|
247
|
-Building an executable JAR
|
|
|
|
248
|
---------------------------
|
|
|
|
249
|
-
|
|
|
|
|
|
248
|
+Build an executable JAR
|
|
|
|
249
|
+-----------------------
|
|
|
|
250
|
+<span class="maven">
|
|
250
|
Add the following to your `pom.xml` file (keeping any existing properties or plugins intact):
|
251
|
Add the following to your `pom.xml` file (keeping any existing properties or plugins intact):
|
|
251
|
|
252
|
|
|
252
|
`pom.xml`
|
253
|
`pom.xml`
|
|
|
|
|
|
|
264
|
</plugins>
|
265
|
</plugins>
|
|
265
|
</build>
|
266
|
</build>
|
|
266
|
```
|
267
|
```
|
|
|
|
268
|
+</span>
|
|
|
|
269
|
+<span class="gradle">
|
|
|
|
270
|
+```groovy
|
|
|
|
271
|
+TODO: gradle syntax
|
|
|
|
272
|
+```
|
|
|
|
273
|
+</span>
|
|
267
|
|
274
|
|
|
268
|
The following will produce a single executable JAR file containing all necessary dependency classes:
|
275
|
The following will produce a single executable JAR file containing all necessary dependency classes:
|
|
|
|
276
|
+<span class="maven">
|
|
269
|
```
|
277
|
```
|
|
270
|
$ mvn package
|
278
|
$ mvn package
|
|
271
|
```
|
279
|
```
|
|
|
|
280
|
+</span>
|
|
|
|
281
|
+<span class="gradle">
|
|
|
|
282
|
+```
|
|
|
|
283
|
+$ gradle build
|
|
|
|
284
|
+```
|
|
|
|
285
|
+</span>
|
|
272
|
|
286
|
|
|
273
|
-Running the Service
|
|
|
|
274
|
--------------------------------------
|
|
|
|
|
|
287
|
+Run the service
|
|
|
|
288
|
+---------------
|
|
275
|
|
289
|
|
|
276
|
Now you can run it from the jar as well, and distribute that as an executable artifact:
|
290
|
Now you can run it from the jar as well, and distribute that as an executable artifact:
|
|
277
|
```
|
291
|
```
|
|
|
|
|
|
|
283
|
Congratulations! You have just developed a simple RESTful service using Spring. This is a basic foundation for building a complete REST API in Spring.
|
297
|
Congratulations! You have just developed a simple RESTful service using Spring. This is a basic foundation for building a complete REST API in Spring.
|
|
284
|
|
298
|
|
|
285
|
|
299
|
|
|
286
|
-Related Resources
|
|
|
|
|
|
300
|
+<span class="related">
|
|
|
|
301
|
+Related resources
|
|
287
|
-----------------
|
302
|
-----------------
|
|
288
|
|
303
|
|
|
289
|
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:
|
304
|
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:
|
|
|
|
|
|
|
294
|
* Securing a REST service with OAuth
|
309
|
* Securing a REST service with OAuth
|
|
295
|
* [Consuming REST services](https://github.com/springframework-meta/gs-consuming-rest-core/blob/master/README.md)
|
310
|
* [Consuming REST services](https://github.com/springframework-meta/gs-consuming-rest-core/blob/master/README.md)
|
|
296
|
* Testing REST services
|
311
|
* Testing REST services
|
|
|
|
312
|
+</span>
|
|
297
|
|
313
|
|
|
298
|
[zip]: https://github.com/springframework-meta/gs-rest-service/archive/master.zip
|
314
|
[zip]: https://github.com/springframework-meta/gs-rest-service/archive/master.zip
|
|
|
|
315
|
+[jdk7]: http://docs.oracle.com/javase/7/docs/webnotes/install/index.html
|