4 回答

TA貢獻1777條經驗 獲得超10個贊
其實簡單粗暴的理解,
就是如果@RestController注解Controller,則返回的內容就是Return 里的內容。
例如:
@RestController
@RequestMapping
public class TestController {
@RequestMapping("/index")
public String index() {
return "user/hello";
}
}
頁面顯示的是user/hello字符串
如果@Controller注解Controller,則返回到指定頁面。
例如:
@Controller
@RequestMapping
public class TestController {
@RequestMapping("/index")
public String index() {
return "user/hello";
}
}
顯示user底下的hello頁面上的內容

TA貢獻1829條經驗 獲得超6個贊
4.0重要的一個新的改進是@RestController注解,它繼承自@Controller注解。4.0之前的版本,Spring MVC的組件都使用@Controller來標識當前類是一個控制器servlet。 使用這個特性,我們可以開發REST服務的時候不需要使用@Controller而專門的@RestController。 當你實現一個RESTful web services的時候,response將一直通過response body發送。為了簡化開發,Spring 4.0提供了一個專門版本的controller。下面我們來看看@RestController實現的定義: @Target(value=TYPE) @Retention(value=RUNTIME) @Documented @Controller @ResponseBody public @interface RestController 官方解釋: A convenience annotation that is itself annotated with @Controller and @ResponseBody. Types that carry this annotation are treated as controllers where @RequestMapping methods assume @ResponseBody semantics by default. ...4.0重要的一個新的改進是@RestController注解,它繼承自@Controller注解。4.0之前的版本,Spring MVC的組件都使用@Controller來標識當前類是一個控制器servlet。 使用這個特性,我們可以開發REST服務的時候不需要使用@Controller而專門的@RestController。 當你實現一個RESTful web services的時候,response將一直通過response body發送。為了簡化開發,Spring 4.0提供了一個專門版本的controller。下面我們來看看@RestController實現的定義: @Target(value=TYPE) @Retention(value=RUNTIME) @Documented @Controller @ResponseBody public @interface RestController 官方解釋: A convenience annotation that is itself annotated with @Controller and @ResponseBody. Types that carry this annotation are treated as controllers where @RequestMapping methods assume @ResponseBody semantics by default. 注解本身使用@Controller和@ResponseBody注解。使用了這個注解的類會被看作一個controller-使用@RequestMapping的方法有一個默認的@ResponseBody注解。 @ResponseBody – As of version 4.0 this annotation can also be added on the type level in which case is inherited and does not need to be added on the method level. @ResponseBody也可以加到類一級,通過繼承方法一級不需要添加。
- 4 回答
- 0 關注
- 3289 瀏覽
添加回答
舉報