|
|
@@ -24,26 +24,98 @@ What you'll need
|
|
24
|
24
|
----------------
|
|
25
|
25
|
|
|
26
|
26
|
- About 15 minutes
|
|
27
|
|
- - {!include#prereq-editor-jdk-buildtools}
|
|
|
27
|
+ - A favorite text editor or IDE
|
|
|
28
|
+ - [JDK 6][jdk] or better
|
|
|
29
|
+ - [Maven 3.0][mvn] or later
|
|
28
|
30
|
|
|
29
|
|
-## {!include#how-to-complete-this-guide}
|
|
|
31
|
+[jdk]: http://www.oracle.com/technetwork/java/javase/downloads/index.html
|
|
|
32
|
+[mvn]: http://maven.apache.org/download.cgi
|
|
|
33
|
+
|
|
|
34
|
+How to complete this guide
|
|
|
35
|
+--------------------------
|
|
|
36
|
+
|
|
|
37
|
+Like all Spring's [Getting Started guides](/getting-started), you can start from scratch and complete each step, or you can bypass basic setup steps that are already familiar to you. Either way, you end up with working code.
|
|
|
38
|
+
|
|
|
39
|
+To **start from scratch**, move on to [Set up the project](#scratch).
|
|
|
40
|
+
|
|
|
41
|
+To **skip the basics**, do the following:
|
|
|
42
|
+
|
|
|
43
|
+ - [Download][zip] and unzip the source repository for this guide, or clone it using [git](/understanding/git):
|
|
|
44
|
+`git clone https://github.com/springframework-meta/{@project-name}.git`
|
|
|
45
|
+ - cd into `{@project-name}/initial`
|
|
|
46
|
+ - Jump ahead to [Create a resource representation class](#initial).
|
|
|
47
|
+
|
|
|
48
|
+**When you're finished**, you can check your results against the code in `{@project-name}/complete`.
|
|
30
|
49
|
|
|
31
|
50
|
|
|
32
|
51
|
<a name="scratch"></a>
|
|
33
|
52
|
Set up the project
|
|
34
|
53
|
------------------
|
|
35
|
54
|
|
|
36
|
|
-{!include#build-system-intro}
|
|
|
55
|
+First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with [Maven](https://maven.apache.org) and [Gradle](http://gradle.org) is included here. If you're not familiar with either, refer to our [Getting Started with Maven](../gs-maven/README.md) or [Getting Started with Gradle](../gs-gradle/README.md) guides.
|
|
37
|
56
|
|
|
38
|
|
-{!include#create-directory-structure-hello}
|
|
|
57
|
+### Create the directory structure
|
|
39
|
58
|
|
|
40
|
|
-### Create a Maven POM
|
|
|
59
|
+In a project directory of your choosing, create the following subdirectory structure; for example, with `mkdir -p src/main/java/hello` on *nix systems:
|
|
41
|
60
|
|
|
42
|
|
-{!include#maven-project-setup-options}
|
|
|
61
|
+ └── src
|
|
|
62
|
+ └── main
|
|
|
63
|
+ └── java
|
|
|
64
|
+ └── hello
|
|
43
|
65
|
|
|
44
|
|
- {!include:initial/pom.xml}
|
|
|
66
|
+### Create a Maven POM
|
|
45
|
67
|
|
|
46
|
|
-{!include#bootstrap-starter-pom-disclaimer}
|
|
|
68
|
+> **ERROR:** Section 'maven-project-setup-options' not found
|
|
|
69
|
+
|
|
|
70
|
+`pom.xml`
|
|
|
71
|
+```xml
|
|
|
72
|
+<?xml version="1.0" encoding="UTF-8"?>
|
|
|
73
|
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
|
74
|
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
|
75
|
+ <modelVersion>4.0.0</modelVersion>
|
|
|
76
|
+
|
|
|
77
|
+ <groupId>org.springframework</groupId>
|
|
|
78
|
+ <artifactId>gs-rest-service</artifactId>
|
|
|
79
|
+ <version>0.1.0</version>
|
|
|
80
|
+
|
|
|
81
|
+ <parent>
|
|
|
82
|
+ <groupId>org.springframework.bootstrap</groupId>
|
|
|
83
|
+ <artifactId>spring-bootstrap-starters</artifactId>
|
|
|
84
|
+ <version>0.5.0.BUILD-SNAPSHOT</version>
|
|
|
85
|
+ </parent>
|
|
|
86
|
+
|
|
|
87
|
+ <dependencies>
|
|
|
88
|
+ <dependency>
|
|
|
89
|
+ <groupId>org.springframework.bootstrap</groupId>
|
|
|
90
|
+ <artifactId>spring-bootstrap-web-starter</artifactId>
|
|
|
91
|
+ </dependency>
|
|
|
92
|
+ <dependency>
|
|
|
93
|
+ <groupId>com.fasterxml.jackson.core</groupId>
|
|
|
94
|
+ <artifactId>jackson-databind</artifactId>
|
|
|
95
|
+ </dependency>
|
|
|
96
|
+ </dependencies>
|
|
|
97
|
+
|
|
|
98
|
+ <!-- TODO: remove once bootstrap goes GA -->
|
|
|
99
|
+ <repositories>
|
|
|
100
|
+ <repository>
|
|
|
101
|
+ <id>spring-snapshots</id>
|
|
|
102
|
+ <url>http://repo.springsource.org/snapshot</url>
|
|
|
103
|
+ <snapshots><enabled>true</enabled></snapshots>
|
|
|
104
|
+ </repository>
|
|
|
105
|
+ </repositories>
|
|
|
106
|
+ <pluginRepositories>
|
|
|
107
|
+ <pluginRepository>
|
|
|
108
|
+ <id>spring-snapshots</id>
|
|
|
109
|
+ <url>http://repo.springsource.org/snapshot</url>
|
|
|
110
|
+ <snapshots><enabled>true</enabled></snapshots>
|
|
|
111
|
+ </pluginRepository>
|
|
|
112
|
+ </pluginRepositories>
|
|
|
113
|
+</project>
|
|
|
114
|
+```
|
|
|
115
|
+
|
|
|
116
|
+TODO: mention that we're using Spring Bootstrap's [_starter POMs_](../gs-bootstrap-starter) here.
|
|
|
117
|
+
|
|
|
118
|
+Note to experienced Maven users who are unaccustomed to using an external parent project: you can take it out later, it's just there to reduce the amount of code you have to write to get started.
|
|
47
|
119
|
|
|
48
|
120
|
|
|
49
|
121
|
<a name="initial"></a>
|
|
|
@@ -65,7 +137,29 @@ The `id` field is a unique identifier for the greeting, and `content` is the tex
|
|
65
|
137
|
|
|
66
|
138
|
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:
|
|
67
|
139
|
|
|
68
|
|
- {!include:complete/src/main/java/hello/Greeting.java}
|
|
|
140
|
+`src/main/java/hello/Greeting.java`
|
|
|
141
|
+```java
|
|
|
142
|
+package hello;
|
|
|
143
|
+
|
|
|
144
|
+public class Greeting {
|
|
|
145
|
+
|
|
|
146
|
+ private final long id;
|
|
|
147
|
+ private final String content;
|
|
|
148
|
+
|
|
|
149
|
+ public Greeting(long id, String content) {
|
|
|
150
|
+ this.id = id;
|
|
|
151
|
+ this.content = content;
|
|
|
152
|
+ }
|
|
|
153
|
+
|
|
|
154
|
+ public long getId() {
|
|
|
155
|
+ return id;
|
|
|
156
|
+ }
|
|
|
157
|
+
|
|
|
158
|
+ public String getContent() {
|
|
|
159
|
+ return content;
|
|
|
160
|
+ }
|
|
|
161
|
+}
|
|
|
162
|
+```
|
|
69
|
163
|
|
|
70
|
164
|
> **Note:** As you'll see in steps below, Spring will use the _Jackson_ JSON library to automatically marshal instances of type `Greeting` into JSON.
|
|
71
|
165
|
|
|
|
@@ -77,7 +171,30 @@ Create a resource controller
|
|
77
|
171
|
|
|
78
|
172
|
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:
|
|
79
|
173
|
|
|
80
|
|
- {!include:complete/src/main/java/hello/GreetingController.java}
|
|
|
174
|
+`src/main/java/hello/GreetingController.java`
|
|
|
175
|
+```java
|
|
|
176
|
+package hello;
|
|
|
177
|
+
|
|
|
178
|
+import java.util.concurrent.atomic.AtomicLong;
|
|
|
179
|
+import org.springframework.stereotype.Controller;
|
|
|
180
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
181
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
182
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
183
|
+
|
|
|
184
|
+@Controller
|
|
|
185
|
+public class GreetingController {
|
|
|
186
|
+
|
|
|
187
|
+ private static final String template = "Hello, %s!";
|
|
|
188
|
+ private final AtomicLong counter = new AtomicLong();
|
|
|
189
|
+
|
|
|
190
|
+ @RequestMapping("/greeting")
|
|
|
191
|
+ public @ResponseBody Greeting greeting(
|
|
|
192
|
+ @RequestParam(value="name", required=false, defaultValue="World") String name) {
|
|
|
193
|
+ return new Greeting(counter.incrementAndGet(),
|
|
|
194
|
+ String.format(template, name));
|
|
|
195
|
+ }
|
|
|
196
|
+}
|
|
|
197
|
+```
|
|
81
|
198
|
|
|
82
|
199
|
This controller is concise and simple, but there's plenty going on under the hood. Let's break it down step by step.
|
|
83
|
200
|
|
|
|
@@ -103,7 +220,23 @@ Although it is possible to package this service as a traditional _web applicatio
|
|
103
|
220
|
|
|
104
|
221
|
### Create a main class
|
|
105
|
222
|
|
|
106
|
|
- {!include:complete/src/main/java/hello/Application.java}
|
|
|
223
|
+`src/main/java/hello/Application.java`
|
|
|
224
|
+```java
|
|
|
225
|
+package hello;
|
|
|
226
|
+
|
|
|
227
|
+import org.springframework.bootstrap.SpringApplication;
|
|
|
228
|
+import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
|
|
229
|
+import org.springframework.context.annotation.ComponentScan;
|
|
|
230
|
+
|
|
|
231
|
+@ComponentScan
|
|
|
232
|
+@EnableAutoConfiguration
|
|
|
233
|
+public class Application {
|
|
|
234
|
+
|
|
|
235
|
+ public static void main(String[] args) {
|
|
|
236
|
+ SpringApplication.run(Application.class, args);
|
|
|
237
|
+ }
|
|
|
238
|
+}
|
|
|
239
|
+```
|
|
107
|
240
|
|
|
108
|
241
|
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]_.
|
|
109
|
242
|
|
|
|
@@ -111,7 +244,37 @@ The `@ComponentScan` annotation tells Spring to search recursively through the `
|
|
111
|
244
|
|
|
112
|
245
|
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.
|
|
113
|
246
|
|
|
114
|
|
-### {!include#build-an-executable-jar}
|
|
|
247
|
+### Build an executable JAR
|
|
|
248
|
+
|
|
|
249
|
+Now that your `Application` class is ready, you simply instruct the build system to create a single, executable jar containing everything. This makes it easy to ship, version, and deploy the service as an application throughout the development lifecycle, across different environments, and so forth.
|
|
|
250
|
+
|
|
|
251
|
+Add the following configuration to your existing Maven POM:
|
|
|
252
|
+
|
|
|
253
|
+`pom.xml`
|
|
|
254
|
+```xml
|
|
|
255
|
+ <properties>
|
|
|
256
|
+ <start-class>hello.Application</start-class>
|
|
|
257
|
+ </properties>
|
|
|
258
|
+
|
|
|
259
|
+ <build>
|
|
|
260
|
+ <plugins>
|
|
|
261
|
+ <plugin>
|
|
|
262
|
+ <groupId>org.apache.maven.plugins</groupId>
|
|
|
263
|
+ <artifactId>maven-shade-plugin</artifactId>
|
|
|
264
|
+ </plugin>
|
|
|
265
|
+ </plugins>
|
|
|
266
|
+ </build>
|
|
|
267
|
+```
|
|
|
268
|
+
|
|
|
269
|
+The `start-class` property tells Maven to create a `META-INF/MANIFEST.MF` file with a `Main-Class: hello.Application` entry. This entry enables you to run the jar with `java -jar`.
|
|
|
270
|
+
|
|
|
271
|
+The [Maven Shade plugin][maven-shade-plugin] extracts classes from all jars on the classpath and builds a single "über-jar", which makes it more convenient to execute and transport your service.
|
|
|
272
|
+
|
|
|
273
|
+Now run the following to produce a single executable JAR file containing all necessary dependency classes and resources:
|
|
|
274
|
+
|
|
|
275
|
+ mvn package
|
|
|
276
|
+
|
|
|
277
|
+[maven-shade-plugin]: https://maven.apache.org/plugins/maven-shade-plugin
|
|
115
|
278
|
|
|
116
|
279
|
|
|
117
|
280
|
Run the service
|