Bläddra i källkod

Replace @Controller/@ResponseBody with shorter @RestController

Greg Turnquist 13 år sedan
förälder
incheckning
d14eff2db9

+ 2
- 3
README.ftl.md Visa fil

@@ -46,7 +46,7 @@ Now you can create a web controller for a simple web application.
46 46
 
47 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 51
 Create an Application class
52 52
 ---------------------------
@@ -326,11 +326,10 @@ On top of that, Spring Boot also has Groovy support, allowing you to build Sprin
326 326
 Create a new file called **app.groovy** and put the following code in it:
327 327
 
328 328
 ```groovy
329
-@Controller
329
+@RestController
330 330
 class ThisWillActuallyRun {
331 331
 
332 332
     @RequestMapping("/")
333
-    @ResponseBody
334 333
     String home() {
335 334
         return "Hello World!"
336 335
     }

+ 5
- 7
README.md Visa fil

@@ -118,22 +118,21 @@ Now you can create a web controller for a simple web application.
118 118
 ```java
119 119
 package hello;
120 120
 
121
-import org.springframework.stereotype.Controller;
121
+import org.springframework.web.bind.annotation.RestController;
122 122
 import org.springframework.web.bind.annotation.RequestMapping;
123
-import org.springframework.web.bind.annotation.ResponseBody;
124 123
 
125
-@Controller
124
+@RestController
126 125
 public class HelloController {
127 126
     
128 127
     @RequestMapping("/")
129
-    public @ResponseBody String index() {
128
+    public String index() {
130 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 137
 Create an Application class
139 138
 ---------------------------
@@ -473,11 +472,10 @@ On top of that, Spring Boot also has Groovy support, allowing you to build Sprin
473 472
 Create a new file called **app.groovy** and put the following code in it:
474 473
 
475 474
 ```groovy
476
-@Controller
475
+@RestController
477 476
 class ThisWillActuallyRun {
478 477
 
479 478
     @RequestMapping("/")
480
-    @ResponseBody
481 479
     String home() {
482 480
         return "Hello World!"
483 481
     }

+ 3
- 4
complete/src/main/java/hello/HelloController.java Visa fil

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

+ 3
- 4
initial/src/main/java/hello/HelloController.java Visa fil

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