GetMapping 和 PostMapping 及 @RestController

在新接触springboot,做小例时,在一个方法上写了PostMapping,然后通过url调用,发现报错405。最后发现是写了PostMapping的问题,而应该写成GetMapping
那么GetMappingPostMapping有什么区别呢?
这是spring4.3引入的新的注解:
GetMappingRequestMapping(method = RequestMethod.GET) 的缩写
PostMappingRequestMapping(method = RequestMethod.POST) 的缩写
同样的还有PutMappingDeleteMapping

HTTP中的GETPOSTPUTDELETE就对应着对这个资源的 ,4个操作。到这里,大家应该有个大概的了解了,GET一般用于获取/查询资源信息,而POST一般用于更新资源信息。

补充一下 : 什么时候是get请求和post请求呢?
Get:
a. 直接在浏览器地址栏输入某个地址
b. 点击链接
c. 表单默认的提交方式
Post:
设置表单method = “post”
所以我我才会报405。
同样还新增了RestController注解
RestController注解相当于ResponseBodyController
那么图解析器 InternalResourceViewResolver将不起所用,返回return的内容,无法返回jsp或html页面。
如果需要返回json等数据,可以使用RestController

猜你喜欢

转载自blog.csdn.net/Demo_gui/article/details/89284222