在新接触springboot,做小例时,在一个方法上写了PostMapping,然后通过url调用,发现报错405。最后发现是写了PostMapping的问题,而应该写成GetMapping。
那么GetMapping 和 PostMapping有什么区别呢?
这是spring4.3引入的新的注解:
GetMapping 为 RequestMapping(method = RequestMethod.GET) 的缩写
PostMapping 为 RequestMapping(method = RequestMethod.POST) 的缩写
同样的还有PutMapping 和 DeleteMapping
HTTP中的GET,POST,PUT,DELETE就对应着对这个资源的 查,改,增,删4个操作。到这里,大家应该有个大概的了解了,GET一般用于获取/查询资源信息,而POST一般用于更新资源信息。
补充一下 : 什么时候是get请求和post请求呢?
Get:
a. 直接在浏览器地址栏输入某个地址
b. 点击链接
c. 表单默认的提交方式
Post:
设置表单method = “post”
所以我我才会报405。
同样还新增了RestController注解
RestController注解相当于ResponseBody + Controller
那么图解析器 InternalResourceViewResolver将不起所用,返回return的内容,无法返回jsp或html页面。
如果需要返回json等数据,可以使用RestController。