美文网首页
Spring - 参数绑定

Spring - 参数绑定

作者: 齐晋 | 来源:发表于2018-12-11 21:23 被阅读24次

    标签:原创 Spring

    更多Spring实战内容,请参考Spring - 实战指南

    概述

    常见的参数绑定方法有:

    • @PathVariable
    • @RequestParam
    • @RequestBody
    • @ModelAttribute

    @PathVariable

    @PathVariable用于解析路径中的变量,如:
    http://localhost:8080/user/12

    @GetMapping("/user/{userId}")
    public Object test2(@PathVariable("userId") Long userId) {
        return userId;
    }
    

    可将路径中的12绑定到userId上

    @RequestParam

    @RequestParam用来绑定url上的参数,如

    http://localhost:8080/test2?name=asdf&id=12

    @GetMapping("/test2")
    public Object test2(@RequestParam("name")String name, @RequestParam("id")Long id) {
        return "hehe";
    }
    

    当然,@RequestParam可以忽略不写

    @RequestBody

    @RequestBody用来绑定POST方法中,body中的数据。
    如:
    假设body中的数据为:

    {
        "id":12,
        "name":"asdf"
    }
    

    通常将body中数据绑定到对象中。如:

    @PostMapping("/test2")
    public Object test2(@RequestBody User user) {
        return user;
    }
    

    @ModelAttribute

    TODO

    绑定原始类型

    假设url为: http://localhost:8080/test2?name=asdf

    Controller撰写方式为:

    @GetMapping("/test2")
    public Object test2(String name) {
        return name;
    }
    

    spring会自动将name绑定到cotroller方法参数列表中。前提是字段名称必须完全一致。

    绑定对象

    假设有类如下:

    @Data
    public class User {
        private Integer id;
        private String name;
    }
    

    绑定URL参数

    假设有url: http://localhost:8080/test2?name=asdf&id=12

    controller撰写方式为:

    @GetMapping("/test2")
    public Object test2(@RequestBody User user) {
        return user;
    }
    

    Spring会自动将name和id属性绑定到User对象中,无需加任何参数或注解。

    绑定Post body数据

    如果我们将数据放到Post Method的body中的话,参数映射需要@RequestBody注解。

    假设body中的数据为:

    {
        "id":12,
        "name":"asdf"
    }
    

    如果使用@RequestBody注解,但是不用对象承接参数的话,绑定的只是json字符串。如下所示:

    @PostMapping("/test2")
    public Object test2(@RequestBody String param) {
        return param;
    }
    

    param的值为上述json的字符串形式。

    使用字符串,还需要反序列化成对象,会比较麻烦。如果直接用对象来承接数据的话,会更简单:

    @PostMapping("/test2")
    public Object test2(@RequestBody User user) {
        return user;
    }
    

    Spring会自动将body中的name和id分别映射到User对象的name和id属性中。

    当然,如果User对象中有数组的话,能绑定成功吗?能!
    假设User对象为:

    @Data
    public class User {
        private Integer id;
        private String name;
        private List<Integer> scores;
    }
    

    body中数据为:

    {
        "scores":[91,98,97]
    }
    

    Controller撰写方式为:

    @PostMapping("/test2")
    public Object test2(@RequestBody User user) {
        return user;
    }
    

    scores也是能成功绑定到User对象中的。

    绑定数组

    方法一:使用对象类型绑定

    @Data
    public class User {
        private List<Integer> scores;
    }
    

    即如果想绑定scores这样一个数组,可以把数组放到一个对象中,使用对象来绑定参数。

    Controller撰写方式为:

    @PostMapping("/test2")
    public Object test2(@RequestBody User user) {
        return user;
    }
    

    方法二:使用@RequestParam

    使用@RequestParam注解绑定数组,需要有一定的规则。url形式需要是这样的:

    http://localhost:8080?score=12&score=23&score=34
    

    或者这样的:

    http://localhost:8080?score=12,23,34
    

    即url的param需要是一样的

    即如果想绑定scores这样一个数组,可以把数组放到一个对象中,使用对象来绑定参数。

    Controller撰写方式为:

    @PostMapping("/test2")
    public Object test2(@RequestParam(value="score") List<Integer>scores) {
        return scores;
    }
    

    这样,可以把参数中的所有score绑定到list上。

    当然,如果有form表单是这样的,也是可以这样绑定的

    <input type="checkbox" name="id" value="12" />
    <input type="checkbox" name="id" value="23" />
    

    绑定Map

    绑定Map和绑定对象类似,都是比较简单的。

    相关文章

      网友评论

          本文标题:Spring - 参数绑定

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