瀏覽代碼

Address TODO items in README.md

Craig Walls 13 年之前
父節點
當前提交
44caa7acd4
共有 5 個檔案被更改,包括 30 行新增16 行删除
  1. 20
    12
      README.md
  2. 1
    0
      complete/.gitignore
  3. 1
    1
      complete/pom.xml
  4. 7
    2
      complete/src/main/java/hello/Main.java
  5. 1
    1
      start/pom.xml

+ 20
- 12
README.md 查看文件

@@ -39,6 +39,11 @@ Add the following within the `<project>` section of your pom.xml file:
39 39
         <version>2.1.4</version>
40 40
     </dependency>
41 41
     <dependency>
42
+        <groupId>com.fasterxml.jackson.core</groupId>
43
+        <artifactId>jackson-databind</artifactId>
44
+        <version>2.1.4</version>
45
+    </dependency>
46
+    <dependency>
42 47
         <groupId>org.apache.tomcat</groupId>
43 48
         <artifactId>tomcat-catalina</artifactId>
44 49
         <version>7.0.39</version>
@@ -64,6 +69,7 @@ Add the following within the `dependencies { }` section of your build.gradle fil
64 69
 ```groovy
65 70
 compile 'org.springframework:spring-webmvc:3.2.2.RELEASE'
66 71
 compile 'com.fasterxml.jackson.core:jackson-core:2.1.4'
72
+compile 'com.fasterxml.jackson.core:jackson-databind:2.1.4'
67 73
 ```
68 74
 
69 75
 
@@ -211,7 +217,7 @@ The magic is in the [`@ResponseBody`](http://static.springsource.org/spring/docs
211 217
 
212 218
 Creating an executable main class
213 219
 ---------------------------------
214
-_**TODO**: explain._
220
+To run the REST service, we'll create a main class that deploys the application to Tomcat.
215 221
 
216 222
 `src/main/java/hello/Main.java`
217 223
 ```java
@@ -248,6 +254,7 @@ public class Main {
248 254
 }
249 255
 ```
250 256
 
257
+As you can see, the `main()` method fires up an embedded Tomcat server and then loads the application, via the `HelloWorldWebAppInitializer`, into Tomcat.
251 258
 
252 259
 
253 260
 Building an executable JAR
@@ -299,14 +306,18 @@ The following will produce a single executable JAR file containing all necessary
299 306
 $ mvn package
300 307
 ```
301 308
 
302
-_**TODO:** this produces a bunch of the following. Fix?_
303
-```
304
-[WARNING] We have a duplicate javax/el/ExpressionFactory$4.class in /Users/cbeams/.m2/repository/org/apache/tomcat/tomcat-el-api/7.0.39/tomcat-el-api-7.0.39.jar
305
-```
309
+> _**TODO:** this produces a bunch of the following. Fix?_
310
+> ```
311
+> [WARNING] We have a duplicate javax/el/ExpressionFactory$4.class in /Users/cbeams/.m2/repository/org/apache/> tomcat/tomcat-el-api/7.0.39/tomcat-el-api-7.0.39.jar
312
+>```
313
+>> It seems to be a quirk with the shade plugin and seems harmless. There is some advice online for removing
314
+>> or minimizing the warnings by adding exclusions to the dependencies. But given that it seems harmless, I'm
315
+>> unsure if we should dwell on this.
306 316
 
307 317
 
308 318
 Running the Service
309 319
 -------------------------------------
320
+Now that the REST service code has been built into an executable JAR file, you can run it like this:
310 321
 
311 322
 ```
312 323
 $ java -jar target/gs-rest-service-0.0.1-SNAPSHOT.jar
@@ -314,14 +325,11 @@ $ java -jar target/gs-rest-service-0.0.1-SNAPSHOT.jar
314 325
 ... service comes up ...
315 326
 ```
316 327
 
317
-_**TODO:** this fails with the following. Fix._
318
-```
319
-Caused by: java.lang.IllegalArgumentException: Failed to register servlet with name 'dispatcher'.Check if there is another servlet registered under the same name.
320
-        at org.springframework.util.Assert.notNull(Assert.java:112)
321
-        at org.springframework.web.servlet.support.AbstractDispatcherServletInitializer.registerDispatcherServlet(AbstractDispatcherServletInitializer.java:98)
322
-```
328
+Once the service starts, you can test it by pointing your web browser at http://localhost:8080/hello-world. Or you can consume it from the command line using curl:
323 329
 
324
-_**TODO:** exercise the service in some meaningful way, e.g. with `curl`._
330
+```sh
331
+$ curl http://localhost:8080/hello-world
332
+```
325 333
 
326 334
 Congratulations! You have just developed a simple REST service using Spring. This is a basic foundation for building a complete REST API in Spring.
327 335
 

+ 1
- 0
complete/.gitignore 查看文件

@@ -4,3 +4,4 @@
4 4
 .settings/
5 5
 bin/
6 6
 build/
7
+target/

+ 1
- 1
complete/pom.xml 查看文件

@@ -14,7 +14,7 @@
14 14
         </dependency>
15 15
         <dependency>
16 16
             <groupId>com.fasterxml.jackson.core</groupId>
17
-            <artifactId>jackson-core</artifactId>
17
+            <artifactId>jackson-databind</artifactId>
18 18
             <version>2.1.4</version>
19 19
         </dependency>
20 20
         <dependency>

+ 7
- 2
complete/src/main/java/hello/Main.java 查看文件

@@ -6,7 +6,6 @@ import org.apache.catalina.Context;
6 6
 import org.apache.catalina.core.AprLifecycleListener;
7 7
 import org.apache.catalina.core.StandardServer;
8 8
 import org.apache.catalina.startup.Tomcat;
9
-import org.springframework.web.SpringServletContainerInitializer;
10 9
 
11 10
 public class Main {
12 11
 
@@ -24,7 +23,13 @@ public class Main {
24 23
 		Context context = tomcat.addWebapp("/", "/");
25 24
 		HashSet<Class<?>> classes = new HashSet<Class<?>>();
26 25
 		classes.add(HelloWorldWebAppInitializer.class);
27
-		context.addServletContainerInitializer(new SpringServletContainerInitializer(), classes);
26
+		
27
+		// TODO: The following line is necessary to run hello.Main in Eclipse or from 'gradlew run'.
28
+		//       But when the executable JAR produced by Maven Shade plugin is run, it results in
29
+		//       an error from the DispatcherServlet being loaded twice with the name "dispatcher".
30
+		//       Need to find a way that works equally well with 'gradlew run'/Eclipse and the
31
+		//       executable JAR.
32
+//		context.addServletContainerInitializer(new SpringServletContainerInitializer(), classes);
28 33
 		tomcat.start();
29 34
 		tomcat.getServer().await();
30 35
 	}

+ 1
- 1
start/pom.xml 查看文件

@@ -14,7 +14,7 @@
14 14
         </dependency>
15 15
         <dependency>
16 16
             <groupId>com.fasterxml.jackson.core</groupId>
17
-            <artifactId>jackson-core</artifactId>
17
+            <artifactId>jackson-databind</artifactId>
18 18
             <version>2.1.4</version>
19 19
         </dependency>
20 20
         <dependency>