|
|
|
|
1
|
-:spring_version: current
|
|
|
2
|
-:toc:
|
|
|
3
|
-:project_id: gs-rest-service
|
|
|
4
|
-:spring_version: current
|
|
|
5
|
-:spring_boot_version: 2.0.3.RELEASE
|
|
|
6
|
-:icons: font
|
|
|
7
|
-:source-highlighter: prettify
|
|
|
8
|
-
|
|
|
9
|
This guide walks you through the process of creating a "hello world" link:/understanding/REST[RESTful web service] with Spring.
|
1
|
This guide walks you through the process of creating a "hello world" link:/understanding/REST[RESTful web service] with Spring.
|
10
|
|
2
|
|
11
|
-== What you'll build
|
|
|
|
|
3
|
+*** What you'll build
|
12
|
|
4
|
|
13
|
You'll build a service that will accept HTTP GET requests at:
|
5
|
You'll build a service that will accept HTTP GET requests at:
|
14
|
|
6
|
|
|
|
|
|
17
|
```
|
9
|
```
|
18
|
and respond with a link:/understanding/JSON[JSON] representation of a greeting:
|
10
|
and respond with a link:/understanding/JSON[JSON] representation of a greeting:
|
19
|
|
11
|
|
20
|
-[source,json]
|
|
|
21
|
-----
|
|
|
|
|
12
|
+
|
|
|
13
|
+```
|
22
|
{"id":1,"content":"Hello, World!"}
|
14
|
{"id":1,"content":"Hello, World!"}
|
23
|
-----
|
|
|
|
|
15
|
+```
|
24
|
|
16
|
|
25
|
You can customize the greeting with an optional `name` parameter in the query string:
|
17
|
You can customize the greeting with an optional `name` parameter in the query string:
|
26
|
|
18
|
|
27
|
-----
|
|
|
|
|
19
|
+```
|
28
|
http://localhost:8080/greeting?name=User
|
20
|
http://localhost:8080/greeting?name=User
|
29
|
-----
|
|
|
|
|
21
|
+```
|
30
|
|
22
|
|
31
|
The `name` parameter value overrides the default value of "World" and is reflected in the response:
|
23
|
The `name` parameter value overrides the default value of "World" and is reflected in the response:
|
32
|
|
24
|
|
33
|
-[source,json]
|
|
|
34
|
-----
|
|
|
|
|
25
|
+
|
|
|
26
|
+```
|
35
|
{"id":1,"content":"Hello, User!"}
|
27
|
{"id":1,"content":"Hello, User!"}
|
36
|
-----
|
|
|
|
|
28
|
+```
|
37
|
|
29
|
|
38
|
|
30
|
|
39
|
-== What you'll need
|
|
|
|
|
31
|
+*** What you'll need
|
40
|
|
32
|
|
41
|
:java_version: 1.8
|
33
|
:java_version: 1.8
|
42
|
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[]
|
34
|
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[]
|
|
|
|
|
53
|
|
45
|
|
54
|
|
46
|
|
55
|
[[initial]]
|
47
|
[[initial]]
|
56
|
-== Create a resource representation class
|
|
|
|
|
48
|
+*** Create a resource representation class
|
57
|
|
49
|
|
58
|
Now that you've set up the project and build system, you can create your web service.
|
50
|
Now that you've set up the project and build system, you can create your web service.
|
59
|
|
51
|
|
|
|
|
|
61
|
|
53
|
|
62
|
The service will handle `GET` requests for `/greeting`, optionally with a `name` parameter in the query string. The `GET` request should return a `200 OK` response with JSON in the body that represents a greeting. It should look something like this:
|
54
|
The service will handle `GET` requests for `/greeting`, optionally with a `name` parameter in the query string. The `GET` request should return a `200 OK` response with JSON in the body that represents a greeting. It should look something like this:
|
63
|
|
55
|
|
64
|
-[source,json]
|
|
|
65
|
-----
|
|
|
|
|
56
|
+
|
|
|
57
|
+```
|
66
|
{
|
58
|
{
|
67
|
"id": 1,
|
59
|
"id": 1,
|
68
|
"content": "Hello, World!"
|
60
|
"content": "Hello, World!"
|
69
|
}
|
61
|
}
|
70
|
-----
|
|
|
|
|
62
|
+```
|
71
|
|
63
|
|
72
|
The `id` field is a unique identifier for the greeting, and `content` is the textual representation of the greeting.
|
64
|
The `id` field is a unique identifier for the greeting, and `content` is the textual representation of the greeting.
|
73
|
|
65
|
|
74
|
To model the greeting representation, you create a resource representation class. Provide a plain old java object with fields, constructors, and accessors for the `id` and `content` data:
|
66
|
To model the greeting representation, you create a resource representation class. Provide a plain old java object with fields, constructors, and accessors for the `id` and `content` data:
|
75
|
|
67
|
|
76
|
`src/main/java/hello/Greeting.java`
|
68
|
`src/main/java/hello/Greeting.java`
|
77
|
-[source,java]
|
|
|
78
|
-----
|
|
|
|
|
69
|
+
|
|
|
70
|
+```
|
79
|
include::complete/src/main/java/hello/Greeting.java[]
|
71
|
include::complete/src/main/java/hello/Greeting.java[]
|
80
|
-----
|
|
|
|
|
72
|
+```
|
81
|
|
73
|
|
82
|
NOTE: As you see in steps below, Spring uses the http://wiki.fasterxml.com/JacksonHome[Jackson JSON] library to automatically marshal instances of type `Greeting` into JSON.
|
74
|
NOTE: As you see in steps below, Spring uses the http://wiki.fasterxml.com/JacksonHome[Jackson JSON] library to automatically marshal instances of type `Greeting` into JSON.
|
83
|
|
75
|
|
84
|
Next you create the resource controller that will serve these greetings.
|
76
|
Next you create the resource controller that will serve these greetings.
|
85
|
|
77
|
|
86
|
|
78
|
|
87
|
-== Create a resource controller
|
|
|
|
|
79
|
+*** Create a resource controller
|
88
|
|
80
|
|
89
|
In Spring's approach to building RESTful web services, HTTP requests are handled by a controller. These components are easily identified by the http://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/web/bind/annotation/RestController.html[`@RestController`] annotation, and the `GreetingController` below handles `GET` requests for `/greeting` by returning a new instance of the `Greeting` class:
|
81
|
In Spring's approach to building RESTful web services, HTTP requests are handled by a controller. These components are easily identified by the http://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/web/bind/annotation/RestController.html[`@RestController`] annotation, and the `GreetingController` below handles `GET` requests for `/greeting` by returning a new instance of the `Greeting` class:
|
90
|
|
82
|
|
91
|
`src/main/java/hello/GreetingController.java`
|
83
|
`src/main/java/hello/GreetingController.java`
|
92
|
-[source,java]
|
|
|
93
|
-----
|
|
|
|
|
84
|
+
|
|
|
85
|
+```
|
94
|
include::complete/src/main/java/hello/GreetingController.java[]
|
86
|
include::complete/src/main/java/hello/GreetingController.java[]
|
95
|
-----
|
|
|
|
|
87
|
+```
|
96
|
|
88
|
|
97
|
This controller is concise and simple, but there's plenty going on under the hood. Let's break it down step by step.
|
89
|
This controller is concise and simple, but there's plenty going on under the hood. Let's break it down step by step.
|
98
|
|
90
|
|
|
|
|
|
111
|
The `Greeting` object must be converted to JSON. Thanks to Spring's HTTP message converter support, you don't need to do this conversion manually. Because http://wiki.fasterxml.com/JacksonHome[Jackson 2] is on the classpath, Spring's http://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.html[`MappingJackson2HttpMessageConverter`] is automatically chosen to convert the `Greeting` instance to JSON.
|
103
|
The `Greeting` object must be converted to JSON. Thanks to Spring's HTTP message converter support, you don't need to do this conversion manually. Because http://wiki.fasterxml.com/JacksonHome[Jackson 2] is on the classpath, Spring's http://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.html[`MappingJackson2HttpMessageConverter`] is automatically chosen to convert the `Greeting` instance to JSON.
|
112
|
|
104
|
|
113
|
|
105
|
|
114
|
-== Make the application executable
|
|
|
|
|
106
|
+*** Make the application executable
|
115
|
|
107
|
|
116
|
Although it is possible to package this service as a traditional link:/understanding/WAR[WAR] file for deployment to an external application server, the simpler approach demonstrated below creates a standalone application. You package everything in a single, executable JAR file, driven by a good old Java `main()` method. Along the way, you use Spring's support for embedding the link:/understanding/Tomcat[Tomcat] servlet container as the HTTP runtime, instead of deploying to an external instance.
|
108
|
Although it is possible to package this service as a traditional link:/understanding/WAR[WAR] file for deployment to an external application server, the simpler approach demonstrated below creates a standalone application. You package everything in a single, executable JAR file, driven by a good old Java `main()` method. Along the way, you use Spring's support for embedding the link:/understanding/Tomcat[Tomcat] servlet container as the HTTP runtime, instead of deploying to an external instance.
|
117
|
|
109
|
|
118
|
|
110
|
|
119
|
`src/main/java/hello/Application.java`
|
111
|
`src/main/java/hello/Application.java`
|
120
|
-[source,java]
|
|
|
121
|
-----
|
|
|
|
|
112
|
+
|
|
|
113
|
+```
|
122
|
include::complete/src/main/java/hello/Application.java[]
|
114
|
include::complete/src/main/java/hello/Application.java[]
|
123
|
-----
|
|
|
|
|
115
|
+```
|
124
|
|
116
|
|
125
|
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/spring-boot-application.adoc[]
|
117
|
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/spring-boot-application.adoc[]
|
126
|
|
118
|
|
|
|
|
|
132
|
Logging output is displayed. The service should be up and running within a few seconds.
|
124
|
Logging output is displayed. The service should be up and running within a few seconds.
|
133
|
|
125
|
|
134
|
|
126
|
|
135
|
-== Test the service
|
|
|
|
|
127
|
+*** Test the service
|
136
|
|
128
|
|
137
|
Now that the service is up, visit http://localhost:8080/greeting, where you see:
|
129
|
Now that the service is up, visit http://localhost:8080/greeting, where you see:
|
138
|
|
130
|
|
139
|
-[source,json]
|
|
|
140
|
-----
|
|
|
|
|
131
|
+
|
|
|
132
|
+```
|
141
|
{"id":1,"content":"Hello, World!"}
|
133
|
{"id":1,"content":"Hello, World!"}
|
142
|
-----
|
|
|
|
|
134
|
+```
|
143
|
|
135
|
|
144
|
Provide a `name` query string parameter with http://localhost:8080/greeting?name=User. Notice how the value of the `content` attribute changes from "Hello, World!" to "Hello User!":
|
136
|
Provide a `name` query string parameter with http://localhost:8080/greeting?name=User. Notice how the value of the `content` attribute changes from "Hello, World!" to "Hello User!":
|
145
|
|
137
|
|
146
|
-[source,json]
|
|
|
147
|
-----
|
|
|
|
|
138
|
+
|
|
|
139
|
+```
|
148
|
{"id":2,"content":"Hello, User!"}
|
140
|
{"id":2,"content":"Hello, User!"}
|
149
|
-----
|
|
|
|
|
141
|
+```
|
150
|
|
142
|
|
151
|
This change demonstrates that the `@RequestParam` arrangement in `GreetingController` is working as expected. The `name` parameter has been given a default value of "World", but can always be explicitly overridden through the query string.
|
143
|
This change demonstrates that the `@RequestParam` arrangement in `GreetingController` is working as expected. The `name` parameter has been given a default value of "World", but can always be explicitly overridden through the query string.
|
152
|
|
144
|
|
153
|
Notice also how the `id` attribute has changed from `1` to `2`. This proves that you are working against the same `GreetingController` instance across multiple requests, and that its `counter` field is being incremented on each call as expected.
|
145
|
Notice also how the `id` attribute has changed from `1` to `2`. This proves that you are working against the same `GreetingController` instance across multiple requests, and that its `counter` field is being incremented on each call as expected.
|
154
|
|
146
|
|
155
|
|
147
|
|
156
|
-== Summary
|
|
|
|
|
148
|
+*** Summary
|
157
|
|
149
|
|
158
|
Congratulations! You've just developed a RESTful web service with Spring.
|
150
|
Congratulations! You've just developed a RESTful web service with Spring.
|
159
|
|
151
|
|
160
|
-== See Also
|
|
|
|
|
152
|
+*** See Also
|
161
|
|
153
|
|
162
|
The following guides may also be helpful:
|
154
|
The following guides may also be helpful:
|
163
|
|
155
|
|
|
|
|
|
179
|
* https://spring.io/guides/gs/rest-hateoas/[Building a Hypermedia-Driven RESTful Web Service]
|
171
|
* https://spring.io/guides/gs/rest-hateoas/[Building a Hypermedia-Driven RESTful Web Service]
|
180
|
* https://spring.io/guides/gs/circuit-breaker/[Circuit Breaker]
|
172
|
* https://spring.io/guides/gs/circuit-breaker/[Circuit Breaker]
|
181
|
|
173
|
|
182
|
-include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/footer.adoc[]
|
|
|