|
|
@@ -31,7 +31,7 @@ Setting Up DispatcherServlet
|
|
31
|
31
|
Spring REST endpoints are built as Spring MVC controllers. Therefore, we'll need to be sure that Spring's DispatcherServlet is configured. We can do that by creating a web application initializer class:
|
|
32
|
32
|
|
|
33
|
33
|
```java
|
|
34
|
|
-package org.springframework.hello;
|
|
|
34
|
+package hello;
|
|
35
|
35
|
|
|
36
|
36
|
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
|
37
|
37
|
|
|
|
@@ -70,7 +70,7 @@ Now that we have setup DispatcherServlet to handle requests for our application,
|
|
70
|
70
|
In our Spring configuration, we'll need to enable annotation-oriented Spring MVC. And we'll also need to tell Spring where it can find our endpoint controller class. The following configuration class takes care of both of those things:
|
|
71
|
71
|
|
|
72
|
72
|
```java
|
|
73
|
|
-package org.springframework.hello;
|
|
|
73
|
+package hello;
|
|
74
|
74
|
import org.springframework.context.annotation.ComponentScan;
|
|
75
|
75
|
import org.springframework.context.annotation.Configuration;
|
|
76
|
76
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
|
@@ -82,7 +82,7 @@ public class HelloWorldConfiguration {
|
|
82
|
82
|
}
|
|
83
|
83
|
```
|
|
84
|
84
|
|
|
85
|
|
-The @EnableWebMvc annotation turns on annotation-oriented Spring MVC. And we've also annotated the configuration class with @ComponentScan to have it look for components (including controllers) in the org.springframework.hello package. As it turns out, classes that are annotated with @Configuration are also discovered by component scanning, so we had to specify an exclude filter to keep it from discovering and using our configuration class a second time.
|
|
|
85
|
+The @EnableWebMvc annotation turns on annotation-oriented Spring MVC. And we've also annotated the configuration class with @ComponentScan to have it look for components (including controllers) in the hello package.
|
|
86
|
86
|
|
|
87
|
87
|
Creating a Representation Class
|
|
88
|
88
|
-------------------------------
|
|
|
@@ -104,7 +104,7 @@ The id field is a unique identifier for the saying, and content is the textual r
|
|
104
|
104
|
To model this representation, we’ll create a representation class:
|
|
105
|
105
|
|
|
106
|
106
|
```java
|
|
107
|
|
-package org.springframework.hello;
|
|
|
107
|
+package hello;
|
|
108
|
108
|
|
|
109
|
109
|
public class Saying {
|
|
110
|
110
|
|
|
|
@@ -134,7 +134,7 @@ Creating a Resource Controller
|
|
134
|
134
|
In Spring, REST endpoints are just Spring MVC controllers. The following Spring MVC controller handles a GET request for /hello-world and returns our Saying resource:
|
|
135
|
135
|
|
|
136
|
136
|
```java
|
|
137
|
|
-package org.springframework.hello;
|
|
|
137
|
+package hello;
|
|
138
|
138
|
import java.util.concurrent.atomic.AtomicLong;
|
|
139
|
139
|
import org.springframework.stereotype.Controller;
|
|
140
|
140
|
import org.springframework.web.bind.annotation.RequestMapping;
|