• @RequestMapping 注解使用技巧


    一、@RequestMapping 基础用法

    用于将任意HTTP 请求映射到控制器方法上。

    @RequestMapping表示共享映射,如果没有指定请求方式,将接收GET、POST、HEAD、OPTIONS、PUT、PATCH、DELETE、TRACE、CONNECT所有的HTTP请求方式。@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@PatchMapping 都是HTTP方法特有的快捷方式@RequestMapping的变体,分别对应具体的HTTP请求方式的映射注解。

    @RequestMapping 注解可以在控制器类上和控制器类中的方法上使用。

    在类的级别上的注解会将一个特定请求或者请求模式映射到一个控制器之上。之后你还可以另外添加方法级别的注解来进一步指定到处理方法的映射关系。

    需要注意的是,控制器方法都应该映射到一个特定的HTTP方法,而不是使用@RequestMapping共享映射。

    在控制器类上和控制器类中的方法上使用

    将映射都放到方法上:

    @RestController
    public class UserController {
    		// 映射到方法上
    		// localhost:8080/user/login
    		// 此处通常用 @GetMapping("/user/login") 表明GET请求方式的映射,因为login登录只需向服务器获取用户数据。
        @RequestMapping("/user/login")  
        public String login() {
            return "user login";
        }
        
        // 映射到方法上
        // localhost:8080/user/register
        // 此处通常用 @PostMapping("/user/login") 表明POST请求方式的映射,因为register注册需要向服务器提交用户数据。
        @RequestMapping("/user/register")
        public String register() {
            return "user register";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    如上述代码所示,到 /user/login 的请求会由 login() 方法来处理,而到 /user/register的请求会由 register() 来处理。

    下面是一个同时在类和方法上应用了 @RequestMapping 注解的示例,上述代码与如下代码等价:

    @RestController
    // 映射到类上
    // localhost:8080/user
    @RequestMapping("/user")
    public class UserController {
    		// 映射到方法上
    		// localhost:8080/user/login
    		// 此处通常用 @GetMapping("/user/login") 表明GET请求方式的映射
        @RequestMapping("/login") 
        public String login() {
            return "user login";
        }
        
        // 映射到方法上
        // localhost:8080/user/register
        // 此处通常用 @PostMapping("/user/login") 表明POST请求方式的映射
        @RequestMapping("/register")
        public String register() {
            return "user register";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    一般情况下,这样代码更规范,因为user的控制器UserController只对user表进行操作。

    二、用 @RequestMapping 处理 HTTP 的各种方法

    Spring MVC 的 @RequestMapping 注解能够处理 HTTP 请求的方法, 比如 GET, PUT, POST, DELETE 以及 PATCH。

    所有的请求默认都会是 HTTP GET 类型的。

    为了能降一个请求映射到一个特定的 HTTP 方法,你需要在 @RequestMapping 中使用 method 属性来声明 HTTP 请求所使用的方法类型,也可以使用等价的组合注解。
    如下所示:

    请求组合注解共享注解
    GET@GetMapping@RequestMapping(method = RequestMethod.GET)
    POST@PostMapping@RequestMapping(method = RequestMethod.POST)
    PUT@PutMapping@RequestMapping(method = RequestMethod.PUT)
    DELETE@DeleteMapping@RequestMapping(method = RequestMethod.DELETE)
    PATCH@PatchMapping@RequestMapping(method = RequestMethod.PATCH)

    需要注意的是,控制器方法都应该映射到一个特定的HTTP方法,即使用组合注解,而不是使用@RequestMapping共享映射。因为组合注解减少了在应用程序上要配置的元数据,并且代码功能更清晰。

    @RestController
    @RequestMapping("/home")
    public class IndexController {
        @RequestMapping(method = RequestMethod.GET)
        String get() {
            return "Hello from get";
        }
        @RequestMapping(method = RequestMethod.DELETE)
        String delete() {
            return "Hello from delete";
        }
        @RequestMapping(method = RequestMethod.POST)
        String post() {
            return "Hello from post";
        }
        @RequestMapping(method = RequestMethod.PUT)
        String put() {
            return "Hello from put";
        }
        @RequestMapping(method = RequestMethod.PATCH)
        String patch() {
            return "Hello from patch";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    在上述这段代码中, @RequestMapping 注解中的 method 元素声明了 HTTP 请求的 HTTP 方法的类型。

    所有的处理处理方法会处理从这同一个 URL( /home)进来的请求, 但要看指定的 HTTP 方法是什么来决定用哪个方法来处理。

    例如,一个 POST 类型的请求 /home 会交给 post() 方法来处理,而一个 DELETE 类型的请求 /home 则会由 delete() 方法来处理。

    三、@RequestMapping 来处理多个 URI

    你可以将多个请求映射到一个方法上去,只需要添加一个带有请求路径值列表的 @RequestMapping 注解就行了。

    @RestController
    @RequestMapping("/home")
    public class IndexController {
    
        @RequestMapping(value = {
            "",
            "/page",
            "page*",
            "view/*,**/msg"
        })
        String indexMultipleMapping() {
            return "Hello from index multiple mapping.";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    如你在这段代码中所看到的,@RequestMapping 支持统配符以及ANT风格的路径。前面这段代码中,如下的这些 URL 都会由 indexMultipleMapping() 来处理:

    localhost:8080/home
    
    localhost:8080/home/
    
    localhost:8080/home/page
    
    localhost:8080/home/pageabc
    
    localhost:8080/home/view/
    
    localhost:8080/home/view/view
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    一些匹配路径示例模式:

    • "/resources/ima?e.p ang " - 匹配路径段中的一个字符
    • “/resources/*.png” - 匹配一个路径段中的零个或多个字符
    • “/resources/**” - 匹配多个路径段
    • “/projects/{project}/versions” - 匹配一个路径段并将其捕获为一个变量
    • “/projects/{project:[a-z]+}/versions” - 用正则表达式匹配并捕获一个变量

    四、带有 @RequestParam 的 @RequestMapping

    @RequestParam 注解配合 @RequestMapping 一起使用,可以将请求的参数同处理方法的参数绑定在一起。

    1、@RequestParam 的 value 属性

    @RequestParam 注解使用的时候可以有一个值,也可以没有值。这个值指定了需要被映射到处理方法参数的请求参数, 代码如下所示:

    @RestController
    @RequestMapping("/home")
    public class IndexController {
    
        @RequestMapping(value = "/id")
        // 实现请求参数 id 与 处理方法参数 personId 的绑定。
        String getIdByValue(@RequestParam("id") String personId) {
            System.out.println("ID is " + personId);
            return "Get ID from query string of URL with value element";
        }
        @RequestMapping(value = "/personId")
        String getId(@RequestParam String personId) {
            System.out.println("ID is " + personId);
            return "Get ID from query string of URL without value element";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    getIdByValue()方法实现了请求参数 id 与 处理方法参数 personId 的绑定。

    getId()方法实现了请求参数 personId 与 处理方法参数 personId 的绑定。

    如果请求参数和处理方法参数的名称一样的话,@RequestParam 注解的 value 这个参数就可省掉了。

    2、@RequestParam 的 required 属性

    @RequestParam 注解的 required 这个参数定义了参数值是否是必须要传的。

    @RestController
    @RequestMapping("/home")
    public class IndexController {
        @RequestMapping(value = "/name")
        String getName(@RequestParam(value = "person", required = true) String personName) {
            return "Required element of request param";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这段代码中,因为 required 被指定为 true,所以 getName() 处理方法对于如下两个 URL 只会对前一个进行处理:

    /home/name?person=xyz
    
    /home/name
    
    
    • 1
    • 2
    • 3
    • 4
    @RestController
    @RequestMapping("/home")
    public class IndexController {
        @RequestMapping(value = "/name")
        String getName(@RequestParam(value = "person", required = false) String personName) {
            return "Required element of request param";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这段代码中,因为 required 被指定为 false,所以 getName() 处理方法对于如下两个 URL 都会进行处理:

    /home/name?person=xyz
    
    /home/name
    
    
    • 1
    • 2
    • 3
    • 4

    3、@RequestParam 的 defaultValue属性

    @RequestParam 的 defaultValue 取值就是用来给取值为空的请求参数提供一个默认值的。

    @RestController
    @RequestMapping("/home")
    public class IndexController {
        @RequestMapping(value = "/name")
        String getName(@RequestParam(value = "person", defaultValue = "John") String personName) {
            return "Required element of request param";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这段代码中,如果 person 这个请求参数为空,那么 getName() 处理方法就会接收 John 这个默认值作为其参数。

    五、用 @RequestMapping 的produces 和 consumes属性来处理生产和消费对象

    可以使用 @RequestMapping 注解的 produces 和 consumes 这两个元素来缩小请求映射类型的范围。

    为了能用请求的媒体类型来产生对象, 你要用到 @RequestMapping 的 produces 元素再结合着 @ResponseBody 注解。

    你也可以利用 @RequestMapping 的 comsumes 元素再结合着 @RequestBody 注解用请求的媒体类型来消费对象。

    下面这段代码就用到的 @RequestMapping 的生产和消费对象元素:

    @RestController
    @RequestMapping("/home")
    public class IndexController {
        @RequestMapping(value = "/prod", produces = {
            "application/JSON"
        })
        @ResponseBody
        String getProduces() {
            return "Produces attribute";
        }
    
        @RequestMapping(value = "/cons", consumes = {
            "application/JSON",
            "application/XML"
        })
        String getConsumes() {
            return "Consumes attribute";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这段代码中,

    getProduces() 处理方法会产生一个 JSON 响应, @ResponseBody 的作用其实是将 java 对象转为 json 格式的数据。

    getConsumes() 处理方法可以同时处理请求中的 JSON 和 SML 内容。

    六、使用 @RequestMapping 的header属性来处理消息头

    @RequestMapping 注解提供了一个 header 元素来根据请求中的消息头内容缩小请求映射的范围。

    在可以指定 header 元素的值,用 myHeader = myValue 这样的格式:

    @RestController
    @RequestMapping("/home")
    public class IndexController {
        @RequestMapping(value = "/head", headers = {
            "content-type=text/plain"
        })
        String post() {
            return "Mapping applied along with headers";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在上面这段代码中, @RequestMapping 注解的 headers 属性将映射范围缩小到了 post() 方法。有了这个,post() 方法就只会处理到 /home/head 并且 content-typeheader 被指定为 text/plain 这个值的请求。

    你也可以像下面这样指定多个消息头:

    @RestController
    @RequestMapping("/home")
    public class IndexController {
        @RequestMapping(value = "/head", headers = {
            "content-type=text/plain",
            "content-type=text/html"
        }) String post() {
            return "Mapping applied along with headers";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    这样, post() 方法就能同时接受 text/plain 还有 text/html 的请求了。

    七、使用 @RequestMapping 的params属性来处理请求参数

    @RequestMapping 直接的 params 元素可以进一步帮助我们缩小请求映射的定位范围。使用 params 元素,你可以让多个处理方法处理到同一个URL 的请求, 而这些请求的参数是不一样的。

    你可以用 myParams = myValue 这种格式来定义参数,也可以使用通配符来指定特定的参数值在请求中是不受支持的。

    @RestController
    @RequestMapping("/home")
    public class IndexController {
        @RequestMapping(value = "/fetch", params = {
            "personId=10"
        })
        String getParams(@RequestParam("personId") String id) {
            return "Fetched parameter using params attribute = " + id;
        }
        @RequestMapping(value = "/fetch", params = {
            "personId=20"
        })
        String getParamsDifferent(@RequestParam("personId") String id) {
            return "Fetched parameter using params attribute = " + id;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在这段代码中,getParams() 和 getParamsDifferent() 两个方法都能处理相同的一个 URL (/home/fetch) ,但是会根据 params 元素的配置不同而决定具体来执行哪一个方法。

    例如,当 URL 是 /home/fetch?id=10 的时候, getParams() 会执行,因为 id 的值是10,。对于 localhost:8080/home/fetch?personId=20 这个URL, getParamsDifferent() 处理方法会得到执行,因为 id 值是 20。

    八、使用 @RequestMapping 处理动态 URI

    @RequestMapping 注解可以同 @PathVaraible 注解一起使用,用来处理动态的 URI,URI 的值可以作为控制器中处理方法的参数。你也可以使用正则表达式来只处理可以匹配到正则表达式的动态 URI。

    @RestController
    @RequestMapping("/home")
    public class IndexController {
        @RequestMapping(value = "/fetch/{id}", method = RequestMethod.GET)
        String getDynamicUriValue(@PathVariable String id) {
            System.out.println("ID is " + id);
            return "Dynamic URI parameter fetched";
        }
        @RequestMapping(value = "/fetch/{id:[a-z]+}/{name}", method = RequestMethod.GET)
        String getDynamicUriValueRegex(@PathVariable("name") String name) {
            System.out.println("Name is " + name);
            return "Dynamic URI parameter fetched using regex";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这段代码中,方法 getDynamicUriValue() 会在发起到 localhost:8080/home/fetch/10 的请求时执行。这里 getDynamicUriValue() 方法 id 参数也会动态地被填充为 10 这个值。

    方法 getDynamicUriValueRegex() 会在发起到 localhost:8080/home/fetch/category/shirt 的请求时执行。不过,如果发起的请求是 /home/fetch/10/shirt 的话,会抛出异常,因为这个URI并不能匹配正则表达式。

    @PathVariable 同 @RequestParam的运行方式不同。你使用 @PathVariable 是为了从 URI 里取到查询参数值。换言之,你使用 @RequestParam 是为了从 URI 模板中获取参数值。

    九、设置 @RequestMapping 默认的处理方法

    在控制器类中,你可以有一个默认的处理方法,它可以在有一个向默认 URI 发起的请求时被执行。

    下面是默认处理方法的示例:

    @RestController
    @RequestMapping("/home")
    public class IndexController {
        @RequestMapping()
        String
        default () {
            return "This is a default method for the class";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这段代码中,向 /home 发起的一个请求将会由 default() 来处理,因为注解并没有指定任何值。

  • 相关阅读:
    瑞吉外卖09-菜品模块的CRUD与启售、停售
    Day4:Linux系统编程1-60P
    高效的 Json 解析框架 kotlinx.serialization
    hive sql 取当周周一 str_to_date(DATE_FORMAT(biz_date, ‘%Y%v‘), ‘%Y%v‘)
    【计算机操作系统慕课版】第一章课后习题
    海康G5系列(armv7l) heop模式下交叉编译Qt qmqtt demo,出现moc缺少高版本GLibc问题之解决
    Eclipse下载、安装、配置教程
    关于Adobe Acrobat Reader升级后界面布局变化-新旧布局的选择切换
    【日常记录】Connection reset
    java使用elasticsearchClient调用es7.17
  • 原文地址:https://blog.csdn.net/t194978/article/details/133206471