|
|
|
|
|
|
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:
|
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
|
```java
|
33
|
```java
|
|
34
|
-package org.springframework.hello;
|
|
|
|
|
|
34
|
+package hello;
|
|
35
|
|
35
|
|
|
36
|
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
36
|
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
|
37
|
|
37
|
|
|
|
|
|
|
|
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:
|
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
|
```java
|
72
|
```java
|
|
73
|
-package org.springframework.hello;
|
|
|
|
|
|
73
|
+package hello;
|
|
74
|
import org.springframework.context.annotation.ComponentScan;
|
74
|
import org.springframework.context.annotation.ComponentScan;
|
|
75
|
import org.springframework.context.annotation.Configuration;
|
75
|
import org.springframework.context.annotation.Configuration;
|
|
76
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
76
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
|
|
|
|
|
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
|
Creating a Representation Class
|
87
|
Creating a Representation Class
|
|
88
|
-------------------------------
|
88
|
-------------------------------
|
|
|
|
|
|
|
104
|
To model this representation, we’ll create a representation class:
|
104
|
To model this representation, we’ll create a representation class:
|
|
105
|
|
105
|
|
|
106
|
```java
|
106
|
```java
|
|
107
|
-package org.springframework.hello;
|
|
|
|
|
|
107
|
+package hello;
|
|
108
|
|
108
|
|
|
109
|
public class Saying {
|
109
|
public class Saying {
|
|
110
|
|
110
|
|
|
|
|
|
|
|
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:
|
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
|
```java
|
136
|
```java
|
|
137
|
-package org.springframework.hello;
|
|
|
|
|
|
137
|
+package hello;
|
|
138
|
import java.util.concurrent.atomic.AtomicLong;
|
138
|
import java.util.concurrent.atomic.AtomicLong;
|
|
139
|
import org.springframework.stereotype.Controller;
|
139
|
import org.springframework.stereotype.Controller;
|
|
140
|
import org.springframework.web.bind.annotation.RequestMapping;
|
140
|
import org.springframework.web.bind.annotation.RequestMapping;
|