Quellcode durchsuchen

Revise instructions and add pom.xml

Chris Beams vor 13 Jahren
Ursprung
Commit
0e5a9e8ccf
2 geänderte Dateien mit 88 neuen und 12 gelöschten Zeilen
  1. 39
    12
      README.md
  2. 49
    0
      start/pom.xml

+ 39
- 12
README.md Datei anzeigen

@@ -1,6 +1,7 @@
1
-Getting Started: Creating a REST Service
2
-========================================
1
+# Getting Started Building a RESTful Web Service
3 2
 
3
+Introduction
4
+------------
4 5
 This Getting Started guide will walk you through the process of creating a simple REST service using Spring.
5 6
 
6 7
 To help you get started, we've provided an initial project structure as well as the completed project for you in GitHub:
@@ -13,21 +14,45 @@ In the `start` folder, you'll find a bare project, ready for you to copy-n-paste
13 14
 
14 15
 Before we can write the REST service itself, there's some initial project setup that's required. Or, you can skip straight to the [fun part](#creating-a-representation-class).
15 16
 
16
-Selecting Dependencies
17
-----------------------
18
-The sample in this Getting Started Guide will leverage Spring MVC and the Jackson JSON processor. Therefore, the following library dependencies are needed in the project's build configuration:
19 17
 
20
-  - org.springframework:spring-webmvc:3.2.2.RELEASE
21
-  - com.fasterxml.jackson.core:jackson-core:2.1.4
18
+Adding dependencies
19
+-------------------
20
+First you'll need to set up a basic build script. You can use any build system you like, but we've included snippets for [Maven](https://maven.apache.org) and [Gradle](http://gradle.org) here. If you're not familiar with either of these, you can refer to our [Getting Started with Maven](../gs-maven/README.md) or [Getting Started with Gradle](../gs-gradle/README.md) guides.
22 21
 
23
-Refer to the [Gradle Getting Started Guide]() or the [Maven Getting Started Guide]() for details on how to include these dependencies in your build.
22
+Add the [Spring MVC](TODO) and [Jackson](http://jackson.codehaus.org) JSON libraries as dependencies:
24 23
 
25
-Setting Up DispatcherServlet
26
-----------------------------
27
-Spring REST 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 web application initializer class:
24
+### Maven \[[copy complete `pom.xml` to clipboard](start/pom.xml)\]
28 25
 
26
+```xml
27
+<dependencies>
28
+    <dependency>
29
+        <groupId>org.springframework</groupId>
30
+        <artifactId>spring-webmvc</artifactId>
31
+        <version>3.2.2.RELEASE</version>
32
+    </dependency>
33
+    <dependency>
34
+        <groupId>com.fasterxml.jackson.core</groupId>
35
+        <artifactId>jackson-core</artifactId>
36
+        <version>2.1.4</version>
37
+    </dependency>
38
+</dependencies>
39
+```
40
+
41
+### Gradle \[[copy complete `build.gradle` to clipboard](start/build.gradle)\]
42
+
43
+```groovy
44
+compile 'org.springframework:spring-webmvc:3.2.2.RELEASE'
45
+compile 'com.fasterxml.jackson.core:jackson-core:2.1.4'
46
+```
47
+
48
+
49
+Setting up the Spring DispatcherServlet
50
+------------------------------------------
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:
52
+
53
+`src/main/java/gs/HelloWorldWebAppInitializer.java`
29 54
 ```java
30
-package hello;
55
+package gs;
31 56
 
32 57
 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
33 58
 
@@ -59,6 +84,7 @@ The `getRootConfigClasses()` and `getServletConfigClasses()` methods specify the
59 84
 
60 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.
61 86
 
87
+
62 88
 Creating a Configuration Class
63 89
 ------------------------------
64 90
 Now that we have setup `DispatcherServlet` to handle requests for our application, we need to configure the Spring application context used by `DispatcherServlet`.
@@ -80,6 +106,7 @@ public class HelloWorldConfiguration {
80 106
 	
81 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.
82 108
 
109
+
83 110
 Creating a Representation Class
84 111
 -------------------------------
85 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.

+ 49
- 0
start/pom.xml Datei anzeigen

@@ -0,0 +1,49 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
+    <modelVersion>4.0.0</modelVersion>
4
+ 
5
+    <groupId>org.springframework</groupId>
6
+    <artifactId>gs-rest-service</artifactId>
7
+    <version>0.0.1-SNAPSHOT</version>
8
+
9
+    <dependencies>
10
+        <dependency>
11
+            <groupId>org.springframework</groupId>
12
+            <artifactId>spring-webmvc</artifactId>
13
+            <version>3.2.2.RELEASE</version>
14
+        </dependency>
15
+        <dependency>
16
+            <groupId>com.fasterxml.jackson.core</groupId>
17
+            <artifactId>jackson-core</artifactId>
18
+            <version>2.1.4</version>
19
+        </dependency>
20
+        <dependency>
21
+            <groupId>javax.servlet</groupId>
22
+            <artifactId>javax.servlet-api</artifactId>
23
+            <version>3.0.1</version>
24
+            <scope>provided</scope>
25
+        </dependency>
26
+    </dependencies>
27
+
28
+    <properties>
29
+        <!-- use UTF-8 for everything -->
30
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
31
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
32
+    </properties>
33
+
34
+    <build>
35
+        <plugins>
36
+            <plugin>
37
+                <groupId>org.apache.maven.plugins</groupId>
38
+                <artifactId>maven-compiler-plugin</artifactId>
39
+                <version>2.3.2</version>
40
+                <!-- compile for Java 1.6 -->
41
+                <configuration>
42
+                    <source>1.6</source>
43
+                    <target>1.6</target>
44
+                    <encoding>UTF-8</encoding>
45
+                </configuration>
46
+            </plugin>
47
+        </plugins>
48
+    </build>
49
+</project>