Просмотр исходного кода

Reorder to ensure compilation works at every step

Ensure the user can execute a `mvn package` after every step, i.e. that
there is never a moment in which the user sees a compilation error in
the IDE or at the command line.
Chris Beams 13 лет назад
Родитель
Сommit
28506f8f33
1 измененных файлов: 28 добавлений и 25 удалений
  1. 28
    25
      README.md

+ 28
- 25
README.md Просмотреть файл

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
 Setting up the Spring DispatcherServlet
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
 ```java
77
 ```java
55
-package gs;
78
+package hello;
56
 
79
 
57
 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
80
 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
58
 
81
 
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.
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
 Creating a Representation Class
111
 Creating a Representation Class
111
 -------------------------------
112
 -------------------------------
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.
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
 
127
 
127
 To model the greeting representation, we’ll create a representation class:
128
 To model the greeting representation, we’ll create a representation class:
128
 
129
 
130
+`src/main/java/hello/Greeting.java`
129
 ```java
131
 ```java
130
 package hello;
132
 package hello;
131
 
133
 
156
 ------------------------------
158
 ------------------------------
157
 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:
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
 ```java
162
 ```java
160
 package hello;
163
 package hello;
161
 import java.util.concurrent.atomic.AtomicLong;
164
 import java.util.concurrent.atomic.AtomicLong;