@Controller和@RestController的区别
发布日期:2021-10-12 17:51:29
浏览次数:13
分类:技术文章
本文共 1339 字,大约阅读时间需要 4 分钟。
@RestController注解
使用@RestController注解,相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面
例子:
@RestController /* @Controller + @ResponseBody*/public class HospitalController { //注入Service服务对象 @Autowired private HospitalService hospitalService; /** * 查询所有医院信息(未分页) */ @RequestMapping(value = "findAllHospital",method = RequestMethod.GET) public ListfindAllHospital(){ List hospitalList= hospitalService.findAllHospital(); return hospitalList; }
@Controller注解
使用@Controller 注解,在对应的方法上,视图解析器可以解析return 的jsp,html页面,并且跳转到相应页面。若返回json等内容到页面,则需要加@ResponseBody注解
(1)返回到指定页面,需要用 @Controller配合视图解析器InternalResourceViewResolver。 (2)返回JSON,XML或自定义mediaType内容到页面,需要在对应的方法上加上@ResponseBody注解。例子:
@Controllerpublic class FileUploadController {//跳转到上传文件的页面@RequestMapping(value="/gouploadimg", method = RequestMethod.GET)public String goUploadImg() {//跳转到 templates 目录下的 uploadimg.htmlreturn "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();
转载地址:https://blog.csdn.net/qq_39387856/article/details/83214618 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年03月24日 00时27分46秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【Leetcode刷题篇】leetcode188 买卖股票的最佳时机IV
2019-04-26
【Leetcode刷题篇/面试篇】经典动态规划-买卖股票问题总结汇总
2019-04-26
【Java锁体系】五、隐式锁和显式锁的区别(Synchronized和Lock的区别)
2019-04-26
【Java锁体系】七、JMM内存模型详解
2019-04-26
【Java锁体系】八、MESI缓存一致性协议讲解
2019-04-26
【面试篇】Java锁体系
2019-04-26
【面试篇】JVM体系
2019-04-26
【Leetcode刷题篇】leetcode406 根据身高重建队列
2019-04-26
【Leetcode刷题篇】leetcode581 最短无序连续子数组
2019-04-26
【Leetcode刷题篇】leetcode538 把二叉搜索树转换为累加树
2019-04-26
【多线程与高并发】线程的优先级是怎么回事?
2019-04-26
【多线程与高并发】Java守护线程是什么?什么是Java的守护线程?
2019-04-26
【Leetcode刷题篇/面试篇】-前缀树(Trie)
2019-04-26
【Leetcode刷题篇】leetcode337 打家劫舍III
2019-04-26
【Leetcode刷题篇】leetcode4 寻找两个正序数组的中位数
2019-04-26
【Leetcode刷题篇】leetcode316 去除重复字母
2019-04-26
【Leetcode刷题篇】leetcode1081 不同字符的最小子序列
2019-04-26
【面试篇】Java网络编程与IO流体系
2019-04-26
【大话Mysql面试】-Mysql的索引为什么要使用B+树,而不是B树,红黑树等之类?
2019-04-26