javaspringrestpostman

How to send both object and parameters in Postman?


I want to send an object of arraylist (in Java) and other parameters as passed by the user to Postman.

Here is my code for controller:

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

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

I know you can use RAW data and JSON for object and x-www-form-urlencoded for parameters, but how to send them together?


Solution

  • You can use @RequestBody and @RequestParam annotations, like for example:

    @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 will automatically deserialize the HttpRequest body (raw JSON from postman) into a java object (StudentRequestModel). @RequestParam will extract your query parameters (in postman you can do a POST to /listEmpPaidSalariesPaged?orderBy=ascending for example) into the defined variables. You can do both at the same time of course in postman.