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請求都應(yīng)該映射到home
方法。@RestController
注解告訴Spring直接將結(jié)果字符串返回給調(diào)用者。
提示
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章節(jié)獲取更多細節(jié)。
更多建議: