SpringMVC笔记4---如何为处理函数配置支持的HTTP方法

在SpringMVC相关的前面的文章中,采用@RequestMapping对处理函数进行注解,默认情况下,这些方法都是对GET方法进行处理,那如何改变这些处理函数支持的HTTP方法?


1 修改IndexController类如下:

package com.gm.springmvc_test.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {
	@RequestMapping(value = "/index/{name}", method = RequestMethod.POST)
	public String indexController(@PathVariable("name") String name) {
		return "欢迎您:  " + name;
	}
}

此时在@RequestMapping参数中加入了一个method参数用来设置HTTP方法。

注意:SpringMVC最新的版本中提供了一种更加简洁的配置HTTP方法的方式,增加了四个标注:

  • @PutMapping
  • @GetMapping
  • @PostMapping
  • @DeleteMapping

如上例中,我们可以改成@PostMapping("index/{name}")


2 在浏览器中输入http://localhost:8080/index/gongmin,发现浏览器中出现如下提示:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Jan 02 10:55:23 CST 2018
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported

提示不支持GET方法,这是因为浏览器默认使用GET方法来进行HTTP的请求。我们可以通过Postman这个插件来模拟请求。


3 使用Postman进行POST请求:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值