如何在邮递员中发送对象和参数?

问题描述 投票:0回答:1

我想发送由用户传递给邮递员的arraylist(java)对象和其他参数。

这是我的控制器代码

        @PostMapping(path = "/listEmpPaidSalariesPaged", produces = "application/json")

        public List<EmployeeSalaryPayment> listEmpPaidSalariesPaged
        (long SID, Collection<Student> student, String orderBy,
        int limit, int offset)

我知道您可以将RAW数据和JSON用于对象和x-www-form-urlencoded作为参数,但是如何将它们一起发送?

java spring api rest postman
1个回答
0
投票

您可以使用@RequestBody和@RequestParam批注,例如:

@PostMapping(path = "/listEmpPaidSalariesPaged", produces = "application/json")
public List<EmployeeSalaryPayment> listEmpPaidSalariesPaged(@RequestBody StudentRequestModel studentRequestModel, @RequestParam(value = "orderBy", defaultValue = "descending") String orderBy, @RequestParam(value = "limit", defaultValue = "25") int limit, @RequestParam(value = "offset", defaultValue = "0") int offset) {
       // controller code
}

@@ RequestBody会自动将HttpRequest主体(邮递员的原始JSON)反序列化为Java对象(StudentRequestModel)。@RequestParam将提取您的查询参数(例如,在邮递员中,您可以对/ listEmpPaidSalariesPaged?orderBy = ascending进行POST)到定义的变量中。当然,您可以在邮递员中同时进行这两个操作。

© www.soinside.com 2019 - 2024. All rights reserved.