parent
b8b883de31
commit
966ee879b1
|
|
@ -334,11 +334,10 @@ a file called `app.groovy`:
|
|||
|
||||
[source,groovy,indent=0,subs="verbatim,quotes,attributes"]
|
||||
----
|
||||
@Controller
|
||||
@RestController
|
||||
class ThisWillActuallyRun {
|
||||
|
||||
@RequestMapping("/")
|
||||
@ResponseBody
|
||||
String home() {
|
||||
return "Hello World!"
|
||||
}
|
||||
|
|
@ -518,12 +517,11 @@ file named `src/main/java/Example.java`:
|
|||
import org.springframework.stereotype.*;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Controller
|
||||
@RestController
|
||||
@EnableAutoConfiguration
|
||||
public class Example {
|
||||
|
||||
@RequestMapping("/")
|
||||
@ResponseBody
|
||||
String home() {
|
||||
return "Hello World!";
|
||||
}
|
||||
|
|
@ -541,18 +539,18 @@ important parts.
|
|||
|
||||
|
||||
[[getting-started-first-application-annotations]]
|
||||
==== The @Controller, @RequestMapping and @ResponseBody annotations
|
||||
The first annotation on our `Example` class is `@Controller`. This is known as a
|
||||
==== The @RestController and @RequestMapping annotations
|
||||
The first annotation on our `Example` class is `@RestController`. This is known as a
|
||||
_stereotype_ annotation. It provides hints for people reading the code, and for Spring,
|
||||
that the class plays a specific role. In this case, our class is a web `@Controller` so
|
||||
Spring will consider it when handling incoming web requests.
|
||||
|
||||
The `@RequestMapping` annotation provides ``routing'' information. It is telling Spring
|
||||
that any HTTP request with the path "`/`" should be mapped to the `home` method. The
|
||||
additional `@ResponseBody` annotation tells Spring to render the resulting string directly
|
||||
`@RestController` annotation tells Spring to render the resulting string directly
|
||||
back to the caller.
|
||||
|
||||
TIP: The `@Controller`, `@RequestMapping` and `@ResponseBody` annotations are Spring MVC
|
||||
TIP: The `@RestController` and`@RequestMapping` annotations are Spring MVC
|
||||
annotations (they are not specific to Spring Boot). See the
|
||||
<{spring-reference}/#mvc>[MVC section] in the Spring
|
||||
Reference Documentation for more details.
|
||||
|
|
|
|||
|
|
@ -82,11 +82,10 @@ Here is an example ``hello world'' web application written in Groovy:
|
|||
|
||||
[source,groovy,indent=0,subs="verbatim,quotes,attributes"]
|
||||
----
|
||||
@Controller
|
||||
@RestController
|
||||
class WebApplication {
|
||||
|
||||
@RequestMapping("/")
|
||||
@ResponseBody
|
||||
String home() {
|
||||
return "Hello World!"
|
||||
}
|
||||
|
|
@ -104,7 +103,7 @@ same way as Maven or Gradle would; but without requiring you to use a build tool
|
|||
|
||||
Spring Boot extends this technique further, and will attempt to deduce which libraries
|
||||
to ``grab'' based on your code. For example, since the `WebApplication` code above uses
|
||||
`@Controller` annotations, ``Tomcat'' and ``Spring MVC'' will be grabbed.
|
||||
`@RestController` annotations, ``Tomcat'' and ``Spring MVC'' will be grabbed.
|
||||
|
||||
The following items are used as ``grab hints'':
|
||||
|
||||
|
|
@ -158,7 +157,7 @@ in the Spring Boot CLI source code to understand exactly how customizations are
|
|||
==== Default import statements
|
||||
To help reduce the size of your Groovy code, several `import` statements are
|
||||
automatically included. Notice how the example above refers to `@Component`,
|
||||
`@Controller`, `@RequestMapping` and `@ResponseBody` without needing to use
|
||||
`@RestController` and `@RequestMapping` without needing to use
|
||||
fully-qualified names or `import` statements.
|
||||
|
||||
TIP: Many Spring annotations will work without using `import` statements. Try running
|
||||
|
|
|
|||
Loading…
Reference in New Issue