springboot controller 参数绑定

本文介绍如何在SpringBoot项目中实现前后端参数的自动绑定,通过示例展示了User和Privileges对象如何接收并处理来自前端的POST请求数据,包括JSON格式的数据处理和list类型参数的接收。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot可以实现前后端参数绑定

普通POST请求

User.java

package cn.ac.iie.bean;

import java.util.List;

public class Privileges {

    private String userName;
    private String authorityType;
    private List<String> authorityApps;

    public Privileges(String userName, String authorityType, List<String> authorityApps) {
        this.userName = userName;
        this.authorityType = authorityType;
        this.authorityApps = authorityApps;
    }

    public Privileges() {
    }

    @Override
    public String toString() {
        return "Privileges{" +
                "userName='" + userName + '\'' +
                ", authorityType='" + authorityType + '\'' +
                ", authorityApps=" + authorityApps +
                '}';
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getAuthorityType() {
        return authorityType;
    }

    public void setAuthorityType(String authorityType) {
        this.authorityType = authorityType;
    }

    public List<String> getAuthorityApps() {
        return authorityApps;
    }

    public void setAuthorityApps(List<String> authorityApps) {
        this.authorityApps = authorityApps;
    }
}

新建UserController.java

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/user/add")
    public Object addUser(User user) {
        System.out.println(user);
        return "CREATED";
    }

}

可以直接使用PostMan来模拟发送请求,

d5c2b487a21a8738239a770583d722c2c63.jpg

POST请求,发送JSON数据格式

新建PrivilegesController.java

@RestController
public class PrivilegesController {

    @Autowired
    private PrivilegesService privilegesService;
    @PostMapping("/privileges/add")
    public Object privilegesAdd(@RequestBody Privileges2 privileges2) {
        System.out.println(privileges2);
        return "success";
    }

}

Privileges.java

package cn.ac.iie.bean;

import java.util.List;

public class Privileges {

    private String userName;
    private String authorityType;
    private List<String> authorityApps;

    @Override
    public String toString() {
        return "Privileges{" +
                "userName='" + userName + '\'' +
                ", authorityType='" + authorityType + '\'' +
                ", authorityApps=" + authorityApps +
                '}';
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getAuthorityType() {
        return authorityType;
    }

    public void setAuthorityType(String authorityType) {
        this.authorityType = authorityType;
    }

    public List<String> getAuthorityApps() {
        return authorityApps;
    }

    public void setAuthorityApps(List<String> authorityApps) {
        this.authorityApps = authorityApps;
    }
}

bean中包含list,因此需要前端传递数组,后台可以通过spring的@RequestBody直接获取出对应的bean。

postman需要准备的: 

  1. 选post请求 
  2. Headers中添加 key: Content-Type value:application/json 
  3. Body中选raw, JSON(application/json) 
  4. 文本框中写入json参数
{
	"userName": "vincent",
	"authorityType": "mac",
	"authorityApps": ["1","2"]
}

8a3e3e3aec38ac615179bdbcf73127e740f.jpg

这样后台就可以收到对象了

### 如何在Spring Boot Controller中传递数组 #### 使用GET请求传递数组参数 当通过URL查询字符串发送数组时,可以使用`@RequestParam`注解来接收数组类型的参数。客户端可以通过重复相同的参数名多次来表示一个数组。 ```java @GetMapping("/getArray") public String getArray(@RequestParam(value = "items", required = false) List<String> items){ return "Received Items: "+items; } ``` 对于上述方法,在发起HTTP GET请求时,URL可能看起来像这样:`/getArray?items=apple&items=orange&items=banana`[^2] #### 使用POST请求传递JSON格式的数组数据 如果采用POST方式提交表单或JSON体中的数组,则应考虑如下实现: - 对于基于表单的数据(`application/x-www-form-urlencoded`),仍然可以用@RequestParam处理; - 当涉及到更复杂的结构化输入比如JSON对象时,推荐的做法是定义相应的DTO(Data Transfer Object),并借助`@RequestBody`绑定整个实体类实例。 下面是一个接受JSON数组的例子: ```java @PostMapping(path="/postArray", consumes="application/json") public ResponseEntity<?> postArray(@RequestBody String[] strings){ Arrays.stream(strings).forEach(System.out::println); return new ResponseEntity<>(HttpStatus.OK); } ``` 此时前端需发送类似这样的payload: ```json ["item1","item2","item3"] ``` 另外一种情况是在路径变量中指定多个值作为数组的一部分,这通常不常见但也支持: ```java @GetMapping("/{ids}") public void handleIds(@PathVariable("ids") Integer... ids){ // Process id list... } ``` 访问形如 `/pathVariableExample/1,2,3` 的链接将会把 `1`, `2`, 和 `3` 解析成整数数组传入到控制器的方法里去[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值