首页 > 学院 > 开发设计 > 正文

@Controller和@RestController的区别

2019-11-06 06:40:21
字体:
来源:转载
供稿:网友

@Controller和@RestController

@RestController

This code uses SPRing 4’s new @RestController annotation, which marks the class as a controller where every method returns a domain object instead of a view. It’s shorthand for @Controller and @ResponseBody rolled together.

看了Spring的官方文档里面是这样说的,大致意思是这是spring的新注解,使用这个标志这个类作为控制器,没个方法返回一个域对象,而不是指定到一个视图,这个新式注解是@Controller和@ResponseBody的结合体

使用@RestController注意

如果在类上面注解了这个类,那么此Controller中的方法无法使用InternalResourceViewResolver视图解析器,如果配置的视图地址,则会返回字符串 例如:return “login” 则不会跳转到login登录页面,则会返回字符串login

如果想返回指定页面那就只能使用@Controller这个标志作为控制器,配合视图解析器InternalResourceViewResolver才行。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表