多学习。

RequestMapping注解

作用

用于请求URL和处理请求方法之间的对应关系

位置


可以放在方法上或类上
放在类上类似于多级目录,例如:user/testRequestMapping

@Controller
@RequestMapping("/user")
public class HelloController {

    @RequestMapping("/testRequestMapping")
    public String testRequestMapping(){
        System.out.println("测试RequestMappring注解...");
        return "success";
    }
}

属性

1.path与value


path与value是对应属性,作用是相同,故我们属性只有路径一个时可直接利用value的省略用法。

2.method


当前方法可以接受哪种请求方式,比如说:get/post

如果用get访问,会405——方法不允许

@RequestMapping(path = "/testRequestMapping", method = {RequestMethod.POST})
    public String testRequestMapping(){
        System.out.println("测试RequestMappring注解...");
        return "success";
    }

3.params


用于指定限制请求参数的条件,支持简单的表达式。要求请求参数的key和value必须和配置是一模一样的。
必须传username属性,否则会报错400

    @RequestMapping(path = "/testRequestMapping", params = {"username"})
    public String testRequestMapping(){
        System.out.println("测试RequestMappring注解...");
        return "success";
    }

传入的username参数,值必须为heihei

@RequestMapping(path = "/testRequestMapping", params = {"username=heihei"})

传入的username参数,值必须不等于heihei

@RequestMapping(path = "/testRequestMapping", params = {"username!heihei"})

4.headers


发送的请求中必须包含的请求头
请求头必须有Accept

@RequestMapping(path = "/testRequestMapping", params = {"username=heihei"},headers = {"Accept"})
posted @   czyaaa  阅读(319)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示