|
@@ -1,6 +1,6 @@
|
1
|
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
|
5
|
You'll build a service that will accept HTTP GET requests at:
|
6
|
6
|
|
|
@@ -28,7 +28,7 @@ The `name` parameter value overrides the default value of "World" and is reflect
|
28
|
28
|
```
|
29
|
29
|
|
30
|
30
|
|
31
|
|
-*** What you'll need
|
|
31
|
+### What you'll need
|
32
|
32
|
|
33
|
33
|
:java_version: 1.8
|
34
|
34
|
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[]
|
|
@@ -45,7 +45,7 @@ include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/
|
45
|
45
|
|
46
|
46
|
|
47
|
47
|
[[initial]]
|
48
|
|
-*** Create a resource representation class
|
|
48
|
+### Create a resource representation class
|
49
|
49
|
|
50
|
50
|
Now that you've set up the project and build system, you can create your web service.
|
51
|
51
|
|
|
@@ -76,7 +76,7 @@ NOTE: As you see in steps below, Spring uses the http://wiki.fasterxml.com/Jacks
|
76
|
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
|
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,7 +103,7 @@ This code uses Spring 4's new http://docs.spring.io/spring/docs/{spring_version}
|
103
|
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
|
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,7 +124,7 @@ include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/
|
124
|
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
|
129
|
Now that the service is up, visit http://localhost:8080/greeting, where you see:
|
130
|
130
|
|
|
@@ -145,11 +145,11 @@ This change demonstrates that the `@RequestParam` arrangement in `GreetingContro
|
145
|
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
|
150
|
Congratulations! You've just developed a RESTful web service with Spring.
|
151
|
151
|
|
152
|
|
-*** See Also
|
|
152
|
+### See Also
|
153
|
153
|
|
154
|
154
|
The following guides may also be helpful:
|
155
|
155
|
|