[Spring] @RequestMapping()

简介

注解在方法上面,表示通过URL路径即可调用其方法。

定义
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
	String name() default "";
	String[] value() default {};
	String[] path() default {};
	RequestMethod[] method() default {};
	String[] params() default {};
	String[] headers() default {};
	String[] consumes() default {};
	String[] produces() default {};
}
语法
  1. @RequestMapping(value = “/login”)

  2. @RequestMapping(path = “/login”, method=RequestMethod.POST)

  3. @RequestMapping(path = “/login”, method={RequestMethod.POST,RequestMethod.GET})

  4. @RequestMapping(path = “/login”, params={“username=kolbe”,“password=123456”})
    该方法将接收 /login 发来的请求,且请求参数必须为 username=kolbe&password=123456

  5. @RequestMapping(path = “/login”, headers=“Host=localhost:8080”)
    该方法将接收 /login 发来的请求,且请求头必须带有:Host=localhost:8080,这里限制只接收本地请求

  6. @RequestMapping(value = “/login”, method = RequestMethod.POST, consumes=“application/json”)
    方法仅处理请求中 Content-Type 请求头为 application/json 类型的请求

  7. @RequestMapping(value = “/login”, method = RequestMethod.GET, produces=“application/json”)
    方法仅处理请求中 Accept 请求头中包含了 application/json 的请求,同时暗示了返回的内容类型为 application/json

  8. 路径参数:

    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    public String show(@PathVariable("id") Integer id) {
        return "success";
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值