1

I need to pass list of beans into my controller.

@RestController
@RequestMapping("/api")
public class MyController {
    @RequestMapping(value = "/my/{id}/method", method = RequestMethod.POST)
    public Result editRequest(
            @RequestParam("first") String first,
            @RequestParam("second") String second,
            @RequestParam("third") String third,
            @RequestParam("items") List<MyBean> items,
            @PathVariable("id") Long id

    ) {
        ...
    }
}

And MyBean:

public class MyBean {
    private Integer fst;
    private String snd;
    private Long thd;
    private Integer fo;

    public MyBean() {
    }

    ... get/set
}

I need to do it from AngularJS, but now I'm testing it in bash with curl. What command do I need?

curl --data "first=-11374862&second=-1000&third=61&items[0][fst]=124231664&items[0][snd]=12&items[0][thd]=123&items[0][fo]=null" http://localhost:8080/api/my/100000/method --header "Content-Type:application/x-www-form-urlencoded"

Will produce error like this:

Required List parameter 'items' is not present
Dmitry
  • 337
  • 2
  • 12

2 Answers2

2

there is no way you can pass the list of objects, i can think of primitives and strings by using @RequestParam("ids[]") Integer[] ids, or else try using the @RequestBody to pass on the list.

chetank
  • 392
  • 3
  • 17
1

you can try the below curl. your present curl command has items as a 2 dimensional array and not a array of objects.

curl --data "first=-11374862&second=-1000&third=61&items[0].fst=124231664&items[0].snd=12&items[0].thd=123&items[0].fo=null" http://localhost:8080/api/my/100000/method --header "Content-Type:application/x-www-form-urlencoded"

Also, you may consider using @RequestBody for mapping request body to objects. Check this post.

Community
  • 1
  • 1
Bharath
  • 259
  • 1
  • 13