美文网首页
@RequestMapping注解作用和示例

@RequestMapping注解作用和示例

作者: 何佳阳 | 来源:发表于2020-02-13 04:08 被阅读0次

    @RequestMapping是一个处理请求地址映射的注解,可以用在类或者方法上面,当用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

    value属性:请求的实际地址

    value的uri值为以下三类:

    A) 可以指定为普通的具体值;

    示例:@RequestMapping(value="/owners", method=RequestMethod.GET)

    B)  可以指定为含有某变量的一类值(URI Template Patterns with Path Variables);

    示例:@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)

    C) 可以指定为含正则表达式的一类值( URI Template Patterns with Regular Expressions);

    示例:@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")

    method属性:制定清清的method类型,GET,POST,PUT,DELETE等

    示例:@RequestMapping(value="/owners", method=RequestMethod.GET)

    produces 属性:指定返回值类型,必须request请求头中的Accept类型中包含该指定类型还可以指定返回值的字符编码

    @RequestMapping(value ="/demonstration/effect", produces ="application/json;charset=UTF-8")

    consumes 属性:指定处理请求的提交内容类型,例如application/json, text/html

    示例:@RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json")

    params属性:指定request中必须包含某些参数值时,才让该方法处理请求

    示例:@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, params="myParam=myValue")

    headers属性:指定request中必须包含某些指定的header值,才让该方法处理请求

    @RequestMapping(value = "/pets", method = RequestMethod.GET, headers="Referer=http://www.ifeng.com/")

    相关文章

      网友评论

          本文标题:@RequestMapping注解作用和示例

          本文链接:https://www.haomeiwen.com/subject/fotkfhtx.html