|
|
@@ -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
|
|