@Controller和@RestController的区别

mac2024-08-17  67

@RestController注解相当于@ResponseBody + @Controller合在一起的作用

用来注释Controller类
@RestController public class NewsController { @Autowired private NewsService newsService; @RequestMapping("/selectNewsByNewsTypeId") public List selectNewsByNewsTypeId(int newsTypeId) { return newsService.selectNewsByNewsTypeId(newsTypeId); } @RequestMapping("/selectNewsById") public News selectNewsById(int newsId) { return newsService.selectNewsById(newsId); }
1.使用@Controller注释

在对应的方法上,视图解析器可以解析return 的jsp,html页面,并且跳转到相应页面

若返回json等内容到页面,则需要加@ResponseBody注解

2.使用@RestController注释

相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面

作者:Dawnzhang 出处:https://www.cnblogs.com/clwydjgs/ 版权:本文版权归作者 转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任

最新回复(0)