1)若是只是使用@RestController注解Controller,则Controller中的方法没法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起做用,返回的内容就是Return 里的内容。html
例如:原本应该到success.jsp页面的,则其显示success.json
2)若是须要返回到指定页面,则须要用 @Controller配合视图解析器InternalResourceViewResolver才行。
3)若是须要返回JSON,XML或自定义mediaType内容到页面,则须要在对应的方法上加上@ResponseBody注解。app
例如:jsp
在对应的方法上,视图解析器能够解析return 的jsp,html页面,而且跳转到相应页面spa
若返回json等内容到页面,则须要加@ResponseBody注解code
@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();
至关于@Controller+@ResponseBody两个注解的结合,返回json数据不须要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器没法解析jsp,html页面htm
@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; }
欢迎你们关注公众号,不定时干货,只作有价值的输出对象
做者:Dawnzhang
出处:https://www.cnblogs.com/clwydjgs/
版权:本文版权归做者
转载:欢迎转载,但未经做者赞成,必须保留此段声明;必须在文章中给出原文链接;不然必究法律责任blog