springmvc 中@Controller和@RestController的区别
1.Controller, RestController的共同點(diǎn)
都是用來表示Spring某個(gè)類的是否可以接收HTTP請求
2.Controller, RestController的不同點(diǎn)
@Controller標(biāo)識(shí)一個(gè)Spring類是Spring MVC controller處理器
@RestController: a convenience annotation that does nothing more than adding the@Controller and@ResponseBody annotations。 @RestController是@Controller和@ResponseBody的結(jié)合體,兩個(gè)標(biāo)注合并起來的作用。
3、如果只是使用@RestController注解Controller,則Controller中的方法無法返回jsp頁面,配置的視圖解析器InternalResourceViewResolver不起作用,返回的內(nèi)容就是Return 里的內(nèi)容。
例如:本來應(yīng)該到success.jsp頁面的,則其顯示success.
4、如果需要返回到指定頁面,則需要用 @Controller配合視圖解析器InternalResourceViewResolver才行。
5、如果需要返回JSON,XML或自定義mediaType內(nèi)容到頁面,則需要在對應(yīng)的方法上加上@ResponseBody注解。
總結(jié)
以上是生活随笔為你收集整理的springmvc 中@Controller和@RestController的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux启动盘制作
- 下一篇: 统计学基础知识点