一,对比@Controller与@RestController
@Controller注解源码:
@Controller与@RestController区别
@RestController注解源码:
@Controller与@RestController区别
官方文档对@RestController的解释:
@RestController is a stereotype annotation that combines @ResponseBody and @Controller.

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

  1. 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return里的内容。

  2. 如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解

二,问题解释
1、为Controller类使用@RestController注解。
controller无法返回jsp页面配置的视图解析器InternalResourceViewResolver不起作用。
例如:return "sayHello"不会返回sayHello.jso页面,而会直接输出sayHello。
2、为controller类使用@controller注解。
返回指定页面,需要@controller配合视图解析器InternalResourceViewResolver才行。
如果需要返回JSON、xml或者自定义mediaType内容到页面显示,需要在对于方法上加上@ResponseBody注解

相关文章:

  • 2021-03-30
  • 2021-12-14
  • 2021-09-02
  • 2021-09-29
  • 2021-07-24
  • 2022-02-08
猜你喜欢
  • 2022-12-23
  • 2021-07-11
  • 2022-12-23
  • 2021-09-22
  • 2022-02-17
  • 2021-11-01
  • 2021-06-21
相关资源
相似解决方案