Explorar el Código

Replace @Controller/@ResponseBody with shorter @RestController

Greg Turnquist hace 13 años
padre
commit
d14eff2db9

+ 2
- 3
README.ftl.md Ver fichero

46
 
46
 
47
     <@snippet path="src/main/java/hello/HelloController.java" prefix="initial"/>
47
     <@snippet path="src/main/java/hello/HelloController.java" prefix="initial"/>
48
     
48
     
49
-The class is flagged as a `@Controller`, meaning it's ready for use by Spring MVC to handle web requests. `@RequestMapping` maps `/` to the `index()` method. When invoked from a browser or using curl on the command line, the method returns pure text thanks to the `@ResponseBody` annotation.
49
+The class is flagged as a `@RestController`, meaning it's ready for use by Spring MVC to handle web requests. `@RequestMapping` maps `/` to the `index()` method. When invoked from a browser or using curl on the command line, the method returns pure text. That's because `@RestController` combines `@Controller` and `@ResponseBody`, two annotations that results in web requests returning data rather than a view.
50
 
50
 
51
 Create an Application class
51
 Create an Application class
52
 ---------------------------
52
 ---------------------------
326
 Create a new file called **app.groovy** and put the following code in it:
326
 Create a new file called **app.groovy** and put the following code in it:
327
 
327
 
328
 ```groovy
328
 ```groovy
329
-@Controller
329
+@RestController
330
 class ThisWillActuallyRun {
330
 class ThisWillActuallyRun {
331
 
331
 
332
     @RequestMapping("/")
332
     @RequestMapping("/")
333
-    @ResponseBody
334
     String home() {
333
     String home() {
335
         return "Hello World!"
334
         return "Hello World!"
336
     }
335
     }

+ 5
- 7
README.md Ver fichero

118
 ```java
118
 ```java
119
 package hello;
119
 package hello;
120
 
120
 
121
-import org.springframework.stereotype.Controller;
121
+import org.springframework.web.bind.annotation.RestController;
122
 import org.springframework.web.bind.annotation.RequestMapping;
122
 import org.springframework.web.bind.annotation.RequestMapping;
123
-import org.springframework.web.bind.annotation.ResponseBody;
124
 
123
 
125
-@Controller
124
+@RestController
126
 public class HelloController {
125
 public class HelloController {
127
     
126
     
128
     @RequestMapping("/")
127
     @RequestMapping("/")
129
-    public @ResponseBody String index() {
128
+    public String index() {
130
         return "Greetings from Spring Boot!";
129
         return "Greetings from Spring Boot!";
131
     }
130
     }
132
     
131
     
133
 }
132
 }
134
 ```
133
 ```
135
     
134
     
136
-The class is flagged as a `@Controller`, meaning it's ready for use by Spring MVC to handle web requests. `@RequestMapping` maps `/` to the `index()` method. When invoked from a browser or using curl on the command line, the method returns pure text thanks to the `@ResponseBody` annotation.
135
+The class is flagged as a `@RestController`, meaning it's ready for use by Spring MVC to handle web requests. `@RequestMapping` maps `/` to the `index()` method. When invoked from a browser or using curl on the command line, the method returns pure text. That's because `@RestController` combines `@Controller` and `@ResponseBody`, two annotations that results in web requests returning data rather than a view.
137
 
136
 
138
 Create an Application class
137
 Create an Application class
139
 ---------------------------
138
 ---------------------------
473
 Create a new file called **app.groovy** and put the following code in it:
472
 Create a new file called **app.groovy** and put the following code in it:
474
 
473
 
475
 ```groovy
474
 ```groovy
476
-@Controller
475
+@RestController
477
 class ThisWillActuallyRun {
476
 class ThisWillActuallyRun {
478
 
477
 
479
     @RequestMapping("/")
478
     @RequestMapping("/")
480
-    @ResponseBody
481
     String home() {
479
     String home() {
482
         return "Hello World!"
480
         return "Hello World!"
483
     }
481
     }

+ 3
- 4
complete/src/main/java/hello/HelloController.java Ver fichero

1
 package hello;
1
 package hello;
2
 
2
 
3
-import org.springframework.stereotype.Controller;
3
+import org.springframework.web.bind.annotation.RestController;
4
 import org.springframework.web.bind.annotation.RequestMapping;
4
 import org.springframework.web.bind.annotation.RequestMapping;
5
-import org.springframework.web.bind.annotation.ResponseBody;
6
 
5
 
7
-@Controller
6
+@RestController
8
 public class HelloController {
7
 public class HelloController {
9
     
8
     
10
     @RequestMapping("/")
9
     @RequestMapping("/")
11
-    public @ResponseBody String index() {
10
+    public String index() {
12
         return "Greetings from Spring Boot!";
11
         return "Greetings from Spring Boot!";
13
     }
12
     }
14
     
13
     

+ 3
- 4
initial/src/main/java/hello/HelloController.java Ver fichero

1
 package hello;
1
 package hello;
2
 
2
 
3
-import org.springframework.stereotype.Controller;
3
+import org.springframework.web.bind.annotation.RestController;
4
 import org.springframework.web.bind.annotation.RequestMapping;
4
 import org.springframework.web.bind.annotation.RequestMapping;
5
-import org.springframework.web.bind.annotation.ResponseBody;
6
 
5
 
7
-@Controller
6
+@RestController
8
 public class HelloController {
7
 public class HelloController {
9
     
8
     
10
     @RequestMapping("/")
9
     @RequestMapping("/")
11
-    public @ResponseBody String index() {
10
+    public String index() {
12
         return "Greetings from Spring Boot!";
11
         return "Greetings from Spring Boot!";
13
     }
12
     }
14
     
13