【转】@Controller和@RestController的区别
知识点:@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
1) 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
2) 如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
例如:
1.使用@Controller 注解,在对应的方法上,视图解析器可以解析return 的jsp,html页面,并且跳转到相应页面
若返回json等内容到页面,则需要加@ResponseBody注解
@CrossOrigin
@Controller
public class FileUploadController {
//跳转到上传文件的页面
@RequestMapping(value="/gouploadimg", method = RequestMethod.GET)
public String goUploadImg() {
//跳转到 templates 目录下的 uploadimg.html
return "uploadimg";
}
//处理文件上传
@RequestMapping(value="/testuploadimg", method = RequestMethod.POST)
public @ResponseBody String uploadImg(@RequestParam("file") MultipartFile file,
HttpServletRequest request) {
System.out.println("调用文件上传方法");
String contentType = file.getContentType();
String fileName = file.getOriginalFilename();
2.@RestController注解,相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面
@CrossOrigin
@RestController /* @Controller + @ResponseBody*/
public class HospitalController {
//注入Service服务对象
@Autowired
private HospitalService hospitalService;
/**
* 查询所有医院信息(未分页)
*/
@RequestMapping(value = "findAllHospital",method = RequestMethod.GET)
public List<Hospital> findAllHospital(){
List<Hospital> hospitalList= hospitalService.findAllHospital();
return hospitalList;
}
【转】@Controller和@RestController的区别的更多相关文章
- @Controller和@RestController的区别?
@Controller和@RestController的区别?官方文档:@RestController is a stereotype annotation that combines @Respon ...
- springmvc 中@Controller和@RestController的区别
@Controller和@RestController的区别? 官方文档:@RestController is a stereotype annotation that combines @Respo ...
- @Controller 和 @RestController的区别
@Controller 和 @RestController的区别 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,配 ...
- @Controller和@RestController的区别(转)
@Controller和@RestController的区别? 官方文档: @RestController is a stereotype annotation that combines @Resp ...
- @Controller 和 @RestController 的区别
@Controller和@RestController的区别? 官方文档:@RestController is a stereotype annotation that combines @Respo ...
- 创建第一个springboot项目、用springboot实现页面跳转、@Controller和@RestController的区别
文章目录 一.第一个spring boot项目 二.spring boot跳转到指定页面 三.怎样将后台的信息传递到前台 四. @Controller和@RestController的区别? 一.第一 ...
- Controller与RestController的区别
在使用Spring系列的框架的时候,相信几乎所有人都会遇见@Controller与@RestController两个注解,那么这两个注解到底有什么区别? 1. 标记有@RestController的类 ...
- @Controller和@RestController的区别
1. Controller, RestController的共同点 都是用来表示spring某个类的是否可以接收HTTP请求 2. Controller, RestController的不同点 @C ...
- Controller和RestController的区别
1. Controller, RestController的共同点 都是用来表示Spring某个类的是否可以接收HTTP请求 2. Controller, RestController的不同点 @C ...
- Spring 的@Controller 和@RestController的区别
@RestController Spring 4.0中新增的特性 @RestController 继承自 @Controller,相当于@ResponseBody + @Controller 1. ...
随机推荐
- URAL 1297 求最长回文字符串
有种简单的方法,数组从左到右扫一遍,每次以当前的点为中心,只要左右相等就往左右走,这算出来的回文字符串是奇数长度的 还有偶数长度的回文字符串就是以当前扫到的点和它左边的点作为中心,然后往左右扫 这是O ...
- Eclipse配置SVN的几种方法及使用详情
此文章对Myeclipse同样适用. 一.在Eclipse里下载Subclipse插件 方法一:从Eclipse Marketplace里面下载 具体操作:打开Eclipse --> Help ...
- jsp内置对象之response、out、config、exception、pageContext。
本文是对Jsp内置对象的response.out.config.exception.pageContext知识点的详细总结. response对象 Response内置对象和request内置对象是相 ...
- Strongly connected-HDU4635
Problem - 4635 http://acm.hdu.edu.cn/showproblem.php?pid=4635 题目大意: n个点,m条边,求最多再加几条边,然后这个图不是强连通 分析: ...
- Combinations(带for循环的DFS)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [HDU5306]Gorgeous Sequence(标记回收线段树)
题意:维护一个序列,支持区间与一个数取min,询问区间最大,询问区间和(序列长度<=1e6) 分析: http://www.shuizilong.com/house/archives/hdu-5 ...
- omnidazzle是mac的画笔工具
先使用命令 brew cask install omnidazzle 试试,不行参考下面: http://macappstore.org/omnidazzle/
- Androidbuttonshape形状资源码实现
1.项目Src下创建drawable 看文档Develop/API Guides/App Resources/Drawable/Shape Drawable 单词:corners : 角 ; gr ...
- C#高级编程五十四天----Lookup类和有序字典
Lookup类 Dictionary<Tkey,TValue>仅仅为每一个键支持一个值.新类Lookup<Tkey,TValue>是.NET3.5中新增的,它类似与Dictio ...
- 8148和8127中的ezsdk和dvrsdk
http://www.dajudeng.com/d2012081009b8a9d5de87101f69f31952c.html