|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+<#assign project_id="gs-rest-service">
|
|
|
2
|
+
|
|
1
|
3
|
# Getting Started: Building a RESTful Web Service
|
|
2
|
4
|
|
|
3
|
5
|
What you'll build
|
|
|
@@ -24,9 +26,10 @@ What you'll need
|
|
24
|
26
|
----------------
|
|
25
|
27
|
|
|
26
|
28
|
- About 15 minutes
|
|
27
|
|
-<@prereq_editor_jdk_buildtools/>
|
|
|
29
|
+ - <@prereq_editor_jdk_buildtools/>
|
|
|
30
|
+
|
|
28
|
31
|
|
|
29
|
|
-<@how_to_complete_this_guide/>
|
|
|
32
|
+## <@how_to_complete_this_guide/>
|
|
30
|
33
|
|
|
31
|
34
|
|
|
32
|
35
|
<a name="scratch"></a>
|
|
|
@@ -39,7 +42,7 @@ Set up the project
|
|
39
|
42
|
|
|
40
|
43
|
### Create a Maven POM
|
|
41
|
44
|
|
|
42
|
|
-<@snippet path="pom.xml" prefix="initial"/>
|
|
|
45
|
+ <@snippet path="pom.xml" prefix="initial"/>
|
|
43
|
46
|
|
|
44
|
47
|
<@bootstrap_starter_pom_disclaimer/>
|
|
45
|
48
|
|
|
|
@@ -63,7 +66,7 @@ The `id` field is a unique identifier for the greeting, and `content` is the tex
|
|
63
|
66
|
|
|
64
|
67
|
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:
|
|
65
|
68
|
|
|
66
|
|
-<@snippet path="src/main/java/hello/Greeting.java" prefix="complete"/>
|
|
|
69
|
+ <@snippet path="src/main/java/hello/Greeting.java" prefix="complete"/>
|
|
67
|
70
|
|
|
68
|
71
|
> **Note:** As you'll see in steps below, Spring will use the _Jackson_ JSON library to automatically marshal instances of type `Greeting` into JSON.
|
|
69
|
72
|
|
|
|
@@ -75,7 +78,7 @@ Create a resource controller
|
|
75
|
78
|
|
|
76
|
79
|
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:
|
|
77
|
80
|
|
|
78
|
|
-<@snippet path="src/main/java/hello/GreetingController.java" prefix="complete"/>
|
|
|
81
|
+ <@snippet path="src/main/java/hello/GreetingController.java" prefix="complete"/>
|
|
79
|
82
|
|
|
80
|
83
|
This controller is concise and simple, but there's plenty going on under the hood. Let's break it down step by step.
|
|
81
|
84
|
|
|
|
@@ -101,7 +104,7 @@ Although it is possible to package this service as a traditional _web applicatio
|
|
101
|
104
|
|
|
102
|
105
|
### Create a main class
|
|
103
|
106
|
|
|
104
|
|
-<@snippet path="src/main/java/hello/Application.java" prefix="complete"/>
|
|
|
107
|
+ <@snippet path="src/main/java/hello/Application.java" prefix="complete"/>
|
|
105
|
108
|
|
|
106
|
109
|
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]_.
|
|
107
|
110
|
|
|
|
@@ -109,7 +112,7 @@ The `@ComponentScan` annotation tells Spring to search recursively through the `
|
|
109
|
112
|
|
|
110
|
113
|
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.
|
|
111
|
114
|
|
|
112
|
|
-<@build_an_executable_jar/>
|
|
|
115
|
+### <@build_an_executable_jar/>
|
|
113
|
116
|
|
|
114
|
117
|
|
|
115
|
118
|
Run the service
|