|
|
@@ -1,6 +1,7 @@
|
|
1
|
|
-Getting Started: Creating a REST Service
|
|
2
|
|
-========================================
|
|
|
1
|
+# Getting Started Building a RESTful Web Service
|
|
3
|
2
|
|
|
|
3
|
+Introduction
|
|
|
4
|
+------------
|
|
4
|
5
|
This Getting Started guide will walk you through the process of creating a simple REST service using Spring.
|
|
5
|
6
|
|
|
6
|
7
|
To help you get started, we've provided an initial project structure as well as the completed project for you in GitHub:
|
|
|
@@ -13,21 +14,45 @@ In the `start` folder, you'll find a bare project, ready for you to copy-n-paste
|
|
13
|
14
|
|
|
14
|
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).
|
|
15
|
16
|
|
|
16
|
|
-Selecting Dependencies
|
|
17
|
|
-----------------------
|
|
18
|
|
-The sample in this Getting Started Guide will leverage Spring MVC and the Jackson JSON processor. Therefore, the following library dependencies are needed in the project's build configuration:
|
|
19
|
17
|
|
|
20
|
|
- - org.springframework:spring-webmvc:3.2.2.RELEASE
|
|
21
|
|
- - com.fasterxml.jackson.core:jackson-core:2.1.4
|
|
|
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.
|
|
22
|
21
|
|
|
23
|
|
-Refer to the [Gradle Getting Started Guide]() or the [Maven Getting Started Guide]() for details on how to include these dependencies in your build.
|
|
|
22
|
+Add the [Spring MVC](TODO) and [Jackson](http://jackson.codehaus.org) JSON libraries as dependencies:
|
|
24
|
23
|
|
|
25
|
|
-Setting Up DispatcherServlet
|
|
26
|
|
-----------------------------
|
|
27
|
|
-Spring REST services are built as Spring MVC controllers. Therefore, we'll need to be sure that Spring's [`DispatcherServlet`](http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html) is configured. We can do that by creating a web application initializer class:
|
|
|
24
|
+### Maven \[[copy complete `pom.xml` to clipboard](start/pom.xml)\]
|
|
28
|
25
|
|
|
|
26
|
+```xml
|
|
|
27
|
+<dependencies>
|
|
|
28
|
+ <dependency>
|
|
|
29
|
+ <groupId>org.springframework</groupId>
|
|
|
30
|
+ <artifactId>spring-webmvc</artifactId>
|
|
|
31
|
+ <version>3.2.2.RELEASE</version>
|
|
|
32
|
+ </dependency>
|
|
|
33
|
+ <dependency>
|
|
|
34
|
+ <groupId>com.fasterxml.jackson.core</groupId>
|
|
|
35
|
+ <artifactId>jackson-core</artifactId>
|
|
|
36
|
+ <version>2.1.4</version>
|
|
|
37
|
+ </dependency>
|
|
|
38
|
+</dependencies>
|
|
|
39
|
+```
|
|
|
40
|
+
|
|
|
41
|
+### Gradle \[[copy complete `build.gradle` to clipboard](start/build.gradle)\]
|
|
|
42
|
+
|
|
|
43
|
+```groovy
|
|
|
44
|
+compile 'org.springframework:spring-webmvc:3.2.2.RELEASE'
|
|
|
45
|
+compile 'com.fasterxml.jackson.core:jackson-core:2.1.4'
|
|
|
46
|
+```
|
|
|
47
|
+
|
|
|
48
|
+
|
|
|
49
|
+Setting up the Spring DispatcherServlet
|
|
|
50
|
+------------------------------------------
|
|
|
51
|
+Spring-based RESTful web services are built as Spring MVC controllers. Therefore, we'll need to be sure that Spring's [`DispatcherServlet`](http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html) is configured. We can do that by creating a [`WebApplicationInitializer`](http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/WebApplicationInitializer.html) class:
|
|
|
52
|
+
|
|
|
53
|
+`src/main/java/gs/HelloWorldWebAppInitializer.java`
|
|
29
|
54
|
```java
|
|
30
|
|
-package hello;
|
|
|
55
|
+package gs;
|
|
31
|
56
|
|
|
32
|
57
|
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
|
33
|
58
|
|
|
|
@@ -59,6 +84,7 @@ The `getRootConfigClasses()` and `getServletConfigClasses()` methods specify the
|
|
59
|
84
|
|
|
60
|
85
|
For our purposes there will only be a servlet application context, so `getRootConfigClasses()` returns `null`. `getServletConfigClasses()`, however, specifies `HelloWorldConfiguration` as the only configuration class.
|
|
61
|
86
|
|
|
|
87
|
+
|
|
62
|
88
|
Creating a Configuration Class
|
|
63
|
89
|
------------------------------
|
|
64
|
90
|
Now that we have setup `DispatcherServlet` to handle requests for our application, we need to configure the Spring application context used by `DispatcherServlet`.
|
|
|
@@ -80,6 +106,7 @@ public class HelloWorldConfiguration {
|
|
80
|
106
|
|
|
81
|
107
|
The [`@EnableWebMvc`](http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/config/annotation/EnableWebMvc.html) annotation turns on annotation-oriented Spring MVC. 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) to have it look for components (including controllers) in the `hello` package.
|
|
82
|
108
|
|
|
|
109
|
+
|
|
83
|
110
|
Creating a Representation Class
|
|
84
|
111
|
-------------------------------
|
|
85
|
112
|
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.
|