0x000 概述:将某个http请求映射到某个方法上。0x001 @RequestMapping:这个注解可以加在某个Controller或者某个方法上,如果加在Controller上

0x000 概述

将某个http请求映射到某个方法上

0x001 @RequestMapping

这个注解可以加在某个Controller或者某个方法上,如果加在Controller上,则这个Controller中的所有路由映射都将会加上这个前缀(下面会有栗子),如果加在方法上,则没有前缀(下面也有栗子)。

@RequestMapping有以下属性

  • value: 请求的URL的路径

  • path: 和value一样

  • method: 请求的方法

  • consumes: 允许的媒体类型,也就是Content-Type

  • produces: 相应的媒体类型,也就是Accept

  • params: 请求参数

  • headers: 请求头部

0x002 路由匹配

  1. 首先编写ControllerController头部的@RestController将这个控制器标注为Rest控制器:

    package com.lyxxxx.rest.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
    }
    登录后复制
  2. 添加方法,并添加URL匹配:

        @RequestMapping()
        public Object hello() {
            return "hello";
        }
    登录后复制
  3. 精确匹配

    @RequestMapping(value = "/hello2")
    public Object hello2() {
        return "hello2";
    }
    登录后复制
  4. 字符模糊匹配

    @RequestMapping(value = "/hello3/*")
    public Object hello3() {
        return "hello3";
    }
    登录后复制
  5. 单字符模糊匹配

    $ curl 127.0.0.1:8080/hello4/1
    hello4
    登录后复制
  6. 全路径模糊匹配

    @RequestMapping(value = "/hello5/**")
    public Object hello5() {
        return "hello5";
    }
    登录后复制
  7. 多个路径匹配

    @RequestMapping(value = {"/hello6", "/hello6/1"})
    public Object hello6() {
        return "hello6";
    }
    登录后复制
  8. 读取配置

    // resources/application.properties
    
    app.name=hello7
    
    // com.lyxxxx.rest.controller.HelloController
    
    @RequestMapping(value = "${app.name}")
    public Object hello7() {
        return "hello7";
    }
    登录后复制

0x003 方法匹配

匹配请求中的method,写在RequestMethod中的枚举值:

  • GET

  • HEAD

  • POST

  • PUT

  • PATCH

  • DELETE

  • OPTIONS

  • TRACE

package com.lyxxxx.rest.controller;

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

@RestController
public class MethodController {
    @RequestMapping(path = "method/get", method = RequestMethod.GET)
    public Object get() {
        return "get";
    }

    @RequestMapping(path = "method/head", method = RequestMethod.HEAD)
    public Object head() {
        return "head";
    }

    @RequestMapping(path = "method/post", method = RequestMethod.POST)
    public Object post() {
        return "post";
    }

    @RequestMapping(path = "method/put", method = RequestMethod.PUT)
    public Object put() {
        return "put";
    }

    @RequestMapping(path = "method/patch", method = RequestMethod.PATCH)
    public Object patch() {
        return "patch";
    }

    @RequestMapping(path = "method/delete", method = RequestMethod.DELETE)
    public Object delete() {
        return "delete";
    }

    @RequestMapping(path = "method/options", method = RequestMethod.OPTIONS)
    public Object options() {
        return "options";
    }

    @RequestMapping(path = "method/trace", method = RequestMethod.TRACE)
    public Object trace() {
        return "trace";
    }


}
登录后复制
$ curl -X GET  127.0.0.1:8080/method/get
get
$ curl -X POST 127.0.0.1:8080/method/post
post
$ curl -X DELETE 127.0.0.1:8080/method/delete
delete
$ curl -X PUT 127.0.0.1:8080/method/put
put
...
登录后复制

0x003 params 匹配

除了可以匹配URLmethod之外,还可以匹配params

package com.lyxxxx.rest.controller;

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

@RestController
public class ParamsController {
    @RequestMapping(path = "/params", params = "userId=1")
    public Object params() {
        return "params";
    }
}
登录后复制
$ curl 127.0.0.1:8080/params?userId=1
params
登录后复制

0x004 说明

以上参考数据:《Spring Boot2精髓 从构建小系统到架构分部署大系统》

相关文章:

mybatis入门基础(四)----输入映射和输出映射

django中“url映射规则”和“服务端响应顺序”

相关视频:

CSS3 入门教程

以上就是怎么将某个http请求映射到某个方法上?SpringBoot入门:URL 映射的详细内容,更多请关注Work网其它相关文章!

08-30 19:29