|
|
|
|
1
|
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.
|
2
|
|
2
|
|
3
|
-*** What you'll build
|
|
|
|
|
3
|
+### What you'll build
|
4
|
|
4
|
|
5
|
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:
|
6
|
|
6
|
|
|
|
|
|
28
|
```
|
28
|
```
|
29
|
|
29
|
|
30
|
|
30
|
|
31
|
-*** What you'll need
|
|
|
|
|
31
|
+### What you'll need
|
32
|
|
32
|
|
33
|
:java_version: 1.8
|
33
|
:java_version: 1.8
|
34
|
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[]
|
|
|
|
|
45
|
|
45
|
|
46
|
|
46
|
|
47
|
[[initial]]
|
47
|
[[initial]]
|
48
|
-*** Create a resource representation class
|
|
|
|
|
48
|
+### Create a resource representation class
|
49
|
|
49
|
|
50
|
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.
|
51
|
|
51
|
|
|
|
|
|
76
|
Next you create the resource controller that will serve these greetings.
|
76
|
Next you create the resource controller that will serve these greetings.
|
77
|
|
77
|
|
78
|
|
78
|
|
79
|
-*** Create a resource controller
|
|
|
|
|
79
|
+### Create a resource controller
|
80
|
|
80
|
|
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:
|
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:
|
82
|
|
82
|
|
|
|
|
|
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.
|
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.
|
104
|
|
104
|
|
105
|
|
105
|
|
106
|
-*** Make the application executable
|
|
|
|
|
106
|
+### Make the application executable
|
107
|
|
107
|
|
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.
|
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.
|
109
|
|
109
|
|
|
|
|
|
124
|
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.
|
125
|
|
125
|
|
126
|
|
126
|
|
127
|
-*** Test the service
|
|
|
|
|
127
|
+### Test the service
|
128
|
|
128
|
|
129
|
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:
|
130
|
|
130
|
|
|
|
|
|
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.
|
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.
|
146
|
|
146
|
|
147
|
|
147
|
|
148
|
-*** Summary
|
|
|
|
|
148
|
+### Summary
|
149
|
|
149
|
|
150
|
Congratulations! You've just developed a RESTful web service with Spring.
|
150
|
Congratulations! You've just developed a RESTful web service with Spring.
|
151
|
|
151
|
|
152
|
-*** See Also
|
|
|
|
|
152
|
+### See Also
|
153
|
|
153
|
|
154
|
The following guides may also be helpful:
|
154
|
The following guides may also be helpful:
|
155
|
|
155
|
|