Преглед на файлове

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
         <version>2.1.4</version>
39
         <version>2.1.4</version>
40
     </dependency>
40
     </dependency>
41
     <dependency>
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
         <groupId>org.apache.tomcat</groupId>
47
         <groupId>org.apache.tomcat</groupId>
43
         <artifactId>tomcat-catalina</artifactId>
48
         <artifactId>tomcat-catalina</artifactId>
44
         <version>7.0.39</version>
49
         <version>7.0.39</version>
64
 ```groovy
69
 ```groovy
65
 compile 'org.springframework:spring-webmvc:3.2.2.RELEASE'
70
 compile 'org.springframework:spring-webmvc:3.2.2.RELEASE'
66
 compile 'com.fasterxml.jackson.core:jackson-core:2.1.4'
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
 
217
 
212
 Creating an executable main class
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
 `src/main/java/hello/Main.java`
222
 `src/main/java/hello/Main.java`
217
 ```java
223
 ```java
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
 Building an executable JAR
260
 Building an executable JAR
299
 $ mvn package
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
 Running the Service
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
 $ java -jar target/gs-rest-service-0.0.1-SNAPSHOT.jar
323
 $ java -jar target/gs-rest-service-0.0.1-SNAPSHOT.jar
314
 ... service comes up ...
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
 Congratulations! You have just developed a simple REST service using Spring. This is a basic foundation for building a complete REST API in Spring.
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
 .settings/
4
 .settings/
5
 bin/
5
 bin/
6
 build/
6
 build/
7
+target/

+ 1
- 1
complete/pom.xml Целия файл

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

+ 7
- 2
complete/src/main/java/hello/Main.java Целия файл

6
 import org.apache.catalina.core.AprLifecycleListener;
6
 import org.apache.catalina.core.AprLifecycleListener;
7
 import org.apache.catalina.core.StandardServer;
7
 import org.apache.catalina.core.StandardServer;
8
 import org.apache.catalina.startup.Tomcat;
8
 import org.apache.catalina.startup.Tomcat;
9
-import org.springframework.web.SpringServletContainerInitializer;
10
 
9
 
11
 public class Main {
10
 public class Main {
12
 
11
 
24
 		Context context = tomcat.addWebapp("/", "/");
23
 		Context context = tomcat.addWebapp("/", "/");
25
 		HashSet<Class<?>> classes = new HashSet<Class<?>>();
24
 		HashSet<Class<?>> classes = new HashSet<Class<?>>();
26
 		classes.add(HelloWorldWebAppInitializer.class);
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
 		tomcat.start();
33
 		tomcat.start();
29
 		tomcat.getServer().await();
34
 		tomcat.getServer().await();
30
 	}
35
 	}

+ 1
- 1
start/pom.xml Целия файл

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