|
|
@@ -24,70 +24,26 @@ What you'll need
|
|
24
|
24
|
----------------
|
|
25
|
25
|
|
|
26
|
26
|
- About 15 minutes
|
|
27
|
|
- - {!snippet:prereq-editor-jdk-buildtools}
|
|
|
27
|
+ - {!include#prereq-editor-jdk-buildtools}
|
|
28
|
28
|
|
|
29
|
|
-## {!snippet:how-to-complete-this-guide}
|
|
|
29
|
+## {!include#how-to-complete-this-guide}
|
|
30
|
30
|
|
|
31
|
31
|
|
|
32
|
32
|
<a name="scratch"></a>
|
|
33
|
33
|
Set up the project
|
|
34
|
34
|
------------------
|
|
35
|
35
|
|
|
36
|
|
-{!snippet:build-system-intro}
|
|
|
36
|
+{!include#build-system-intro}
|
|
37
|
37
|
|
|
38
|
|
-{!snippet:create-directory-structure-hello}
|
|
|
38
|
+{!include#create-directory-structure-hello}
|
|
39
|
39
|
|
|
40
|
40
|
### Create a Maven POM
|
|
41
|
41
|
|
|
42
|
|
-{!snippet:maven-project-setup-options}
|
|
43
|
|
-
|
|
44
|
|
-`pom.xml`
|
|
45
|
|
-```xml
|
|
46
|
|
-<?xml version="1.0" encoding="UTF-8"?>
|
|
47
|
|
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
48
|
|
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
49
|
|
- <modelVersion>4.0.0</modelVersion>
|
|
50
|
|
-
|
|
51
|
|
- <groupId>org.springframework</groupId>
|
|
52
|
|
- <artifactId>gs-rest-service</artifactId>
|
|
53
|
|
- <version>0.1.0</version>
|
|
54
|
|
-
|
|
55
|
|
- <parent>
|
|
56
|
|
- <groupId>org.springframework.bootstrap</groupId>
|
|
57
|
|
- <artifactId>spring-bootstrap-starters</artifactId>
|
|
58
|
|
- <version>0.5.0.BUILD-SNAPSHOT</version>
|
|
59
|
|
- </parent>
|
|
60
|
|
-
|
|
61
|
|
- <dependencies>
|
|
62
|
|
- <dependency>
|
|
63
|
|
- <groupId>org.springframework.bootstrap</groupId>
|
|
64
|
|
- <artifactId>spring-bootstrap-web-starter</artifactId>
|
|
65
|
|
- </dependency>
|
|
66
|
|
- <dependency>
|
|
67
|
|
- <groupId>com.fasterxml.jackson.core</groupId>
|
|
68
|
|
- <artifactId>jackson-databind</artifactId>
|
|
69
|
|
- </dependency>
|
|
70
|
|
- </dependencies>
|
|
71
|
|
-
|
|
72
|
|
- <!-- TODO: remove once bootstrap goes GA -->
|
|
73
|
|
- <repositories>
|
|
74
|
|
- <repository>
|
|
75
|
|
- <id>spring-snapshots</id>
|
|
76
|
|
- <url>http://repo.springsource.org/snapshot</url>
|
|
77
|
|
- <snapshots><enabled>true</enabled></snapshots>
|
|
78
|
|
- </repository>
|
|
79
|
|
- </repositories>
|
|
80
|
|
- <pluginRepositories>
|
|
81
|
|
- <pluginRepository>
|
|
82
|
|
- <id>spring-snapshots</id>
|
|
83
|
|
- <url>http://repo.springsource.org/snapshot</url>
|
|
84
|
|
- <snapshots><enabled>true</enabled></snapshots>
|
|
85
|
|
- </pluginRepository>
|
|
86
|
|
- </pluginRepositories>
|
|
87
|
|
-</project>
|
|
88
|
|
-```
|
|
89
|
|
-
|
|
90
|
|
-{!snippet:bootstrap-starter-pom-disclaimer}
|
|
|
42
|
+{!include#maven-project-setup-options}
|
|
|
43
|
+
|
|
|
44
|
+ {!include:initial/pom.xml}
|
|
|
45
|
+
|
|
|
46
|
+{!include#bootstrap-starter-pom-disclaimer}
|
|
91
|
47
|
|
|
92
|
48
|
|
|
93
|
49
|
<a name="initial"></a>
|
|
|
@@ -109,29 +65,7 @@ The `id` field is a unique identifier for the greeting, and `content` is the tex
|
|
109
|
65
|
|
|
110
|
66
|
To model the greeting representation, you create a _resource representation class_. To do this, you simply create a plain old java object with fields, constructors, and accessors for the `id` and `content` data:
|
|
111
|
67
|
|
|
112
|
|
-`src/main/java/hello/Greeting.java`
|
|
113
|
|
-```java
|
|
114
|
|
-package hello;
|
|
115
|
|
-
|
|
116
|
|
-public class Greeting {
|
|
117
|
|
-
|
|
118
|
|
- private final long id;
|
|
119
|
|
- private final String content;
|
|
120
|
|
-
|
|
121
|
|
- public Greeting(long id, String content) {
|
|
122
|
|
- this.id = id;
|
|
123
|
|
- this.content = content;
|
|
124
|
|
- }
|
|
125
|
|
-
|
|
126
|
|
- public long getId() {
|
|
127
|
|
- return id;
|
|
128
|
|
- }
|
|
129
|
|
-
|
|
130
|
|
- public String getContent() {
|
|
131
|
|
- return content;
|
|
132
|
|
- }
|
|
133
|
|
-}
|
|
134
|
|
-```
|
|
|
68
|
+ {!include:complete/src/main/java/hello/Greeting.java}
|
|
135
|
69
|
|
|
136
|
70
|
> **Note:** As you'll see in steps below, Spring will use the _Jackson_ JSON library to automatically marshal instances of type `Greeting` into JSON.
|
|
137
|
71
|
|
|
|
@@ -143,30 +77,7 @@ Create a resource controller
|
|
143
|
77
|
|
|
144
|
78
|
In Spring's approach to building RESTful web services, HTTP requests are handled by a _controller_. These components are easily identified by the [`@Controller`][] annotation, and the `GreetingController` below handles `GET` requests for `/greeting` by returning a new instance of the `Greeting` class:
|
|
145
|
79
|
|
|
146
|
|
-`src/main/java/hello/GreetingController.java`
|
|
147
|
|
-```java
|
|
148
|
|
-package hello;
|
|
149
|
|
-
|
|
150
|
|
-import java.util.concurrent.atomic.AtomicLong;
|
|
151
|
|
-import org.springframework.stereotype.Controller;
|
|
152
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
153
|
|
-import org.springframework.web.bind.annotation.RequestParam;
|
|
154
|
|
-import org.springframework.web.bind.annotation.ResponseBody;
|
|
155
|
|
-
|
|
156
|
|
-@Controller
|
|
157
|
|
-public class GreetingController {
|
|
158
|
|
-
|
|
159
|
|
- private static final String template = "Hello, %s!";
|
|
160
|
|
- private final AtomicLong counter = new AtomicLong();
|
|
161
|
|
-
|
|
162
|
|
- @RequestMapping("/greeting")
|
|
163
|
|
- public @ResponseBody Greeting greeting(
|
|
164
|
|
- @RequestParam(value="name", required=false, defaultValue="World") String name) {
|
|
165
|
|
- return new Greeting(counter.incrementAndGet(),
|
|
166
|
|
- String.format(template, name));
|
|
167
|
|
- }
|
|
168
|
|
-}
|
|
169
|
|
-```
|
|
|
80
|
+ {!include:complete/src/main/java/hello/GreetingController.java}
|
|
170
|
81
|
|
|
171
|
82
|
This controller is concise and simple, but there's plenty going on under the hood. Let's break it down step by step.
|
|
172
|
83
|
|
|
|
@@ -192,31 +103,15 @@ Although it is possible to package this service as a traditional _web applicatio
|
|
192
|
103
|
|
|
193
|
104
|
### Create a main class
|
|
194
|
105
|
|
|
195
|
|
-`src/main/java/hello/Application.java`
|
|
|
106
|
+ {!include:complete/src/main/java/hello/Application.java}
|
|
196
|
107
|
|
|
197
|
|
-```java
|
|
198
|
|
-package hello;
|
|
199
|
|
-
|
|
200
|
|
-import org.springframework.bootstrap.SpringApplication;
|
|
201
|
|
-import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
|
202
|
|
-import org.springframework.context.annotation.ComponentScan;
|
|
203
|
|
-
|
|
204
|
|
-@ComponentScan
|
|
205
|
|
-@EnableAutoConfiguration
|
|
206
|
|
-public class Application {
|
|
207
|
|
-
|
|
208
|
|
- public static void main(String[] args) {
|
|
209
|
|
- SpringApplication.run(Application.class, args);
|
|
210
|
|
- }
|
|
211
|
|
-}
|
|
212
|
|
-```
|
|
213
|
108
|
The `main()` method defers to the [`SpringApplication`][] helper class, providing `Application.class` as an argument to its `run()` method. This tells Spring to read the annotation metadata from `Application` and to manage it as a component in the _[Spring application context][u-application-context]_.
|
|
214
|
109
|
|
|
215
|
110
|
The `@ComponentScan` annotation tells Spring to search recursively through the `hello` package and its children for classes marked directly or indirectly with Spring's [`@Component`][] annotation. This directive ensures that Spring finds and registers the `GreetingController`, because it is marked with `@Controller`, which in turn is a kind of `@Component` annotation.
|
|
216
|
111
|
|
|
217
|
112
|
The [`@EnableAutoConfiguration`][] annotation switches on reasonable default behaviors based on the content of your classpath. For example, because the application depends on the embeddable version of Tomcat (tomcat-embed-core.jar), a Tomcat server is set up and configured with reasonable defaults on your behalf. And because the application also depends on Spring MVC (spring-webmvc.jar), a Spring MVC [`DispatcherServlet`][] is configured and registered for you — no `web.xml` necessary! Auto-configuration is a powerful, flexible mechanism. See the [API documentation][`@EnableAutoConfiguration`] for further details.
|
|
218
|
113
|
|
|
219
|
|
-### {!snippet:build-an-executable-jar}
|
|
|
114
|
+### {!include#build-an-executable-jar}
|
|
220
|
115
|
|
|
221
|
116
|
|
|
222
|
117
|
Run the service
|