@RestController 与 @Controller 注解区别
文章来源:https://www.cnblogs.com/hello-tl/p/9202658.html
@RestController注解相当于@ResponseBody + @Controller合在一起的作用
// 一般用于接口 或 前后端分离
1.如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp,html页面,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是return 里的内容。
// 一般用于后台页面
2.如果需要返回到指定页面,则需要用 @Controller 配合视图解析器 InternalResourceViewResolver 才行。
如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
例如
1.如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp,html页面,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是return 里的内容。
package com.web.TestRestController; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.ResponseBody; @RestController
@RequestMapping("/TestController")
public class TestRestController{ // 返回 return 里面的内容
@RequestMapping(value = "index", method = RequestMethod.GET)
public String index(){
// 返回 return 里面的内容 如字符串 json xml 或自定义返回
return "{}";
}
}
2.如果需要返回到指定页面,则需要用 @Controller 配合视图解析器 InternalResourceViewResolver 才行。
如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
package com.web.TestController; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/TestController")
public class TestController{ // 映射文件
@RequestMapping(value = "index", method = RequestMethod.GET)
public String index(){
// 他就会映射到 TestController 目录下 index.jsp 或 index.html 文件
return "TestController/index";
} // 返回 return 里面的内容
@RequestMapping(value = "index", method = RequestMethod.GET)
@ResponseBody
public String index(){
// 返回 return 里面的内容 如字符串 json xml 或自定义返回
return "{}";
}
}
文章来源:https://www.cnblogs.com/hello-tl/p/9202658.html
@RestController 与 @Controller 注解区别的更多相关文章
- 注解@RestController与@Controller的区别
开发RESTful API 时,一般都会在Controller上加上@Controller注解,但是有时候加上@RestController,当同事问为什么的时候,我也一脸懵逼,默默的看了资料,现在就 ...
- @RestController和@Controller注解的区别
@RestController是@ResponseBody和@Controller注解的结合,当你return一个页面时,使用@Controller注解返回的是相应页面,使用@RestControll ...
- RestController 和Controller的区别
restful风格,restcontroller与controller 初步接触springmvc的时候,被要求使用restful风格,彼时一头雾水,不懂何谓restful,参阅了很多资料,慢慢的接触 ...
- @restcontroller与@controller的区别
这段时间偷偷看了下spring boot.结果引用模板时没注意,把@restcontroller替换了@controlle,结果模板出不来.终究原因是spring的知识不到位. 下面说说这2的说明和区 ...
- 浅谈@RestController和@Controller的区别
在做Spring MVC开发时,如果对@RestController或者@Controller这两个注解理解不够清晰的话,就难免会出现用混的情况.而混用的结果往往是无法实现期望的跳转结果或者是直接将跳 ...
- SpringBoot 中 @RestController 和 @Controller 的区别
1 - 在springboot中,@RestController 相当于 @Controller + @ResponseBody;2 - 即在Controller类中,若想返回jsp或html页面,则 ...
- RestController和Controller的区别
知识点:@RestController注解相当于@ResponseBody + @Controller合在一起的作用. 1) 如果只是使用@RestController注解Controller,则Co ...
- SpringMVC中@RestController和@Controller的区别
在使用SpringMVC时,对于Controller中的注解@RestController和@Controller需要我们区分清楚 @RestController注解相当于@ResponseBody和 ...
- @Component注解、@Service注解、@Repository注解、@Controller注解区别
--------------------------------------------------------------------------------------------------- ...
随机推荐
- OpenCv图像像素操作
1:像素 有两种直接操作像素点的方法: 第一种: 将其转化为numpy.array格式,直接进行操作. 第二种:使用Opencv提供的Get1D,Get2D等函数. 2:获取行和列像素 有一下四个函数 ...
- mysql用户和授权
CREATE USER 'monitor'@'10.224.32.%' IDENTIFIED BY '123@abAB'; mysql> GRANT select,insert,update O ...
- python之类的相关名词解释
变量:在类里面定义的变量,不必实例化即可调用 实例变量:在类里面定义的变量,必须实例化之后才可以调用 比如: 属性方法:调用时看起来像是一个变量,方法没有入参,可以变成一个属性方法 在方法上添加@pr ...
- 简单几何(求交点) UVA 11178 Morley's Theorem
题目传送门 题意:莫雷定理,求三个点的坐标 分析:训练指南P259,用到了求角度,向量旋转,求射线交点 /*********************************************** ...
- Eclipse显示空白符,及使用google代码格式化
启动Eclipse,打开Preferences对话框.菜单“window”-“Preferences”. 找到Text Editors,勾选show whitespace characters,如图: ...
- VS配置本地IIS以域名访问
1.IIS下配置自己的网站,添加主机名 2.修改hosts文件(C://Windows/System32/drivers/etc) 3.VS中配置项目Web服务器(选择外部主机)
- flex和box兼容性写法
display: -webkit-box; /* Chrome 4+, Safari 3.1, iOS Safari 3.2+ */ display: -moz-box; /* Firefox 17- ...
- BeanUtils.copyProperties(productInfo, productInfoVO);
一:spring的工具类方法:BeanUtils.copyProperties(orderMasterDTO, orderMasterDO); 作用:将orderMasterDTO对象中的属性值,赋值 ...
- 从源码中无法看出函数所在的js脚本的解决方法
通过设置断点调试使js脚本自动出现
- 禁用DRM
10G: alter system set "_gc_policy_time"=0 scope=spfile sid='*'; alter system set "_gc ...