11.3.1 @RestController和@RequestMapping注解
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 considers it when handling incoming web requests.
第一个注解,在Example
类上的,是@RestController
。这是一个被称做“stereotype”的注解。它为阅读代码的人提供了提示,也为Spring提供了提示,这个类(class)扮演了一个特定的角色。在此情况下,我们的类是一个Web@Controller
, 因此Spring会在处理Web请求时考虑它。
The @RequestMapping
annotation provides “routing” information. It tells Spring that any HTTP request with the /
path should be mapped to the home
method. The @RestController
annotation tells Spring to render the resulting string directly back to the caller.
@RequestMapping
注解提供了“路由”信息。 它告诉Spring,任意带有/
路径的HTTP请求都应该映射到home
方法。@RestController
注解告诉Spring直接将结果字符串返回给调用者。
提示
The
@RestController
and@RequestMapping
annotations are Spring MVC annotations. (They are not specific to Spring Boot.) See the MVC section in the Spring Reference Documentation for more details.
@RestController
和@RequestMapping
注解都是Spring MVC的注解。(它们不是SpringBoot特定的。)参见Spring参考文档中MVC章节获取更多细节。
更多建议: