【问题标题】:How to send both object and parameters in postman?如何在邮递员中发送对象和参数?
【发布时间】:2020-05-22 02:44:48
【问题描述】:

我想将用户传递给邮递员的 arraylist(in 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】:

    您可以使用@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)到定义的变量中。 当然,您可以在邮递员中同时进行这两项操作。

    【讨论】:

      猜你喜欢
      • 2016-09-08
      • 1970-01-01
      • 2021-06-12
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 2017-08-17
      • 2014-12-29
      • 2019-11-14
      相关资源
      最近更新 更多