|
|
@@ -6,13 +6,13 @@ This Getting Started guide will walk you through the process of creating a simpl
|
|
6
|
6
|
To help you get started, we've provided an initial project structure for you in GitHub:
|
|
7
|
7
|
|
|
8
|
8
|
```sh
|
|
9
|
|
-$ git clone git://github.com/SpringSource/gs-rest-service.git
|
|
|
9
|
+$ git clone https://github.com/springframework-meta/gs-rest-service.git
|
|
10
|
10
|
```
|
|
11
|
11
|
|
|
12
|
12
|
As you work through this guide, you can fill in this project with the code necessary to complete the guide. Or, if you'd prefer to see the end result, the completed project is available in the *completed* branch of the Git repository:
|
|
13
|
13
|
|
|
14
|
14
|
```sh
|
|
15
|
|
-$ git clone -b completed git://github.com/SpringSource/gs-rest-service.git
|
|
|
15
|
+$ git clone -b completed https://github.com/springframework-meta/gs-rest-service.git
|
|
16
|
16
|
```
|
|
17
|
17
|
|
|
18
|
18
|
Before we can write the REST endpoint itself, there's some initial project setup that's required. Or, you can skip straight to the [fun part]().
|
|
|
@@ -22,7 +22,7 @@ Selecting Dependencies
|
|
22
|
22
|
The sample in this Getting Started Guide will leverage Spring MVC and the Jackson JSON processor. Therefore, you'll need to declare the following library dependencies in your build:
|
|
23
|
23
|
|
|
24
|
24
|
- org.springframework:spring-webmvc:3.2.2.RELEASE
|
|
25
|
|
- - org.codehaus.jackson:jackson-mapper-asl:1.9.9
|
|
|
25
|
+ - com.fasterxml.jackson.core:jackson-core:2.1.4
|
|
26
|
26
|
|
|
27
|
27
|
Click here for details on how to map these dependencies to your specific build tool.
|
|
28
|
28
|
|
|
|
@@ -90,7 +90,7 @@ With the essential Spring MVC configuration out of the way, it's time to get to
|
|
90
|
90
|
|
|
91
|
91
|
Before we get too carried away with building the endpoint controller, we need to give some thought to what our API will look like.
|
|
92
|
92
|
|
|
93
|
|
-What we want is to handle GET requests for /hello-world, optionally with a name query parameter. In response to such a request, we'd like to send back JSON looking something like this:
|
|
|
93
|
+What we want is to handle GET requests for /hello-world, optionally with a name query parameter. In response to such a request, we'd like to send back JSON, representing a greeting, that looks something like this:
|
|
94
|
94
|
|
|
95
|
95
|
```json
|
|
96
|
96
|
{
|
|
|
@@ -99,19 +99,19 @@ What we want is to handle GET requests for /hello-world, optionally with a name
|
|
99
|
99
|
}
|
|
100
|
100
|
```
|
|
101
|
101
|
|
|
102
|
|
-The id field is a unique identifier for the saying, and content is the textual representation of the saying.
|
|
|
102
|
+The id field is a unique identifier for the greeting, and content is the textual representation of the greeting.
|
|
103
|
103
|
|
|
104
|
|
-To model this representation, we’ll create a representation class:
|
|
|
104
|
+To model the greeting representation, we’ll create a representation class:
|
|
105
|
105
|
|
|
106
|
106
|
```java
|
|
107
|
107
|
package hello;
|
|
108
|
108
|
|
|
109
|
|
-public class Saying {
|
|
|
109
|
+public class Greeting {
|
|
110
|
110
|
|
|
111
|
111
|
private final long id;
|
|
112
|
112
|
private final String content;
|
|
113
|
113
|
|
|
114
|
|
- public Saying(long id, String content) {
|
|
|
114
|
+ public Greeting(long id, String content) {
|
|
115
|
115
|
this.id = id;
|
|
116
|
116
|
this.content = content;
|
|
117
|
117
|
}
|
|
|
@@ -144,14 +144,14 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|
144
|
144
|
|
|
145
|
145
|
@Controller
|
|
146
|
146
|
@RequestMapping("/hello-world")
|
|
147
|
|
-public class HelloWorldResource {
|
|
|
147
|
+public class HelloWorldController {
|
|
148
|
148
|
|
|
149
|
149
|
private static final String template = "Hello, %s!";
|
|
150
|
150
|
private final AtomicLong counter = new AtomicLong();
|
|
151
|
151
|
|
|
152
|
152
|
@RequestMapping(method=RequestMethod.GET)
|
|
153
|
|
- public @ResponseBody Saying sayHello(@RequestParam(value="name", required=false, defaultValue="Stranger") String name) {
|
|
154
|
|
- return new Saying(counter.incrementAndGet(), String.format(template, name));
|
|
|
153
|
+ public @ResponseBody Greeting sayHello(@RequestParam(value="name", required=false, defaultValue="Stranger") String name) {
|
|
|
154
|
+ return new Greeting(counter.incrementAndGet(), String.format(template, name));
|
|
155
|
155
|
}
|
|
156
|
156
|
|
|
157
|
157
|
}
|