|
|
@@ -46,13 +46,36 @@ compile 'com.fasterxml.jackson.core:jackson-core:2.1.4'
|
|
46
|
46
|
```
|
|
47
|
47
|
|
|
48
|
48
|
|
|
|
49
|
+Creating a Configuration Class
|
|
|
50
|
+------------------------------
|
|
|
51
|
+The first step is to set up a simple Spring configuration class. It'll look like this:
|
|
|
52
|
+
|
|
|
53
|
+`src/main/java/hello/HelloWorldConfiguration.java`
|
|
|
54
|
+
|
|
|
55
|
+```java
|
|
|
56
|
+package hello;
|
|
|
57
|
+
|
|
|
58
|
+import org.springframework.context.annotation.ComponentScan;
|
|
|
59
|
+import org.springframework.context.annotation.Configuration;
|
|
|
60
|
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
|
61
|
+
|
|
|
62
|
+@Configuration
|
|
|
63
|
+@EnableWebMvc
|
|
|
64
|
+@ComponentScan
|
|
|
65
|
+public class HelloWorldConfiguration {
|
|
|
66
|
+}
|
|
|
67
|
+```
|
|
|
68
|
+
|
|
|
69
|
+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).
|
|
|
70
|
+
|
|
|
71
|
+
|
|
49
|
72
|
Setting up the Spring DispatcherServlet
|
|
50
|
73
|
------------------------------------------
|
|
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:
|
|
|
74
|
+Spring's [`DispatcherServlet`](http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html) will do the work of accepting incoming HTTP requests and routing them to our controller. The simplest way to configure and register the `DispatcherServlet` is with a [`WebApplicationInitializer`](http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/WebApplicationInitializer.html) class as follows:
|
|
52
|
75
|
|
|
53
|
|
-`src/main/java/gs/HelloWorldWebAppInitializer.java`
|
|
|
76
|
+`src/main/java/hello/HelloWorldWebAppInitializer.java`
|
|
54
|
77
|
```java
|
|
55
|
|
-package gs;
|
|
|
78
|
+package hello;
|
|
56
|
79
|
|
|
57
|
80
|
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
|
58
|
81
|
|
|
|
@@ -85,28 +108,6 @@ The `getRootConfigClasses()` and `getServletConfigClasses()` methods specify the
|
|
85
|
108
|
For our purposes there will only be a servlet application context, so `getRootConfigClasses()` returns `null`. `getServletConfigClasses()`, however, specifies `HelloWorldConfiguration` as the only configuration class.
|
|
86
|
109
|
|
|
87
|
110
|
|
|
88
|
|
-Creating a Configuration Class
|
|
89
|
|
-------------------------------
|
|
90
|
|
-Now that we have setup `DispatcherServlet` to handle requests for our application, we need to configure the Spring application context used by `DispatcherServlet`.
|
|
91
|
|
-
|
|
92
|
|
-In our Spring configuration, we'll need to enable annotation-oriented Spring MVC. And we'll also need to tell Spring where it can find our endpoint controller class. The following configuration class takes care of both of those things:
|
|
93
|
|
-
|
|
94
|
|
-```java
|
|
95
|
|
-package hello;
|
|
96
|
|
-import org.springframework.context.annotation.ComponentScan;
|
|
97
|
|
-import org.springframework.context.annotation.Configuration;
|
|
98
|
|
-import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
99
|
|
-
|
|
100
|
|
-@Configuration
|
|
101
|
|
-@EnableWebMvc
|
|
102
|
|
-@ComponentScan
|
|
103
|
|
-public class HelloWorldConfiguration {
|
|
104
|
|
-}
|
|
105
|
|
-```
|
|
106
|
|
-
|
|
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.
|
|
108
|
|
-
|
|
109
|
|
-
|
|
110
|
111
|
Creating a Representation Class
|
|
111
|
112
|
-------------------------------
|
|
112
|
113
|
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.
|
|
|
@@ -126,6 +127,7 @@ The `id` field is a unique identifier for the greeting, and `content` is the tex
|
|
126
|
127
|
|
|
127
|
128
|
To model the greeting representation, we’ll create a representation class:
|
|
128
|
129
|
|
|
|
130
|
+`src/main/java/hello/Greeting.java`
|
|
129
|
131
|
```java
|
|
130
|
132
|
package hello;
|
|
131
|
133
|
|
|
|
@@ -156,6 +158,7 @@ Creating a Resource Controller
|
|
156
|
158
|
------------------------------
|
|
157
|
159
|
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:
|
|
158
|
160
|
|
|
|
161
|
+`src/main/java/hello/HelloWorldController.java`
|
|
159
|
162
|
```java
|
|
160
|
163
|
package hello;
|
|
161
|
164
|
import java.util.concurrent.atomic.AtomicLong;
|