springrestspring-mvchttp-posthttprequest

Spring MVC @RequestParam a list of objects


I want to create a page where a person sees a list of users and there are check boxes next to each of them that the person can click to have them deleted.

In my MVC that consumes a REST API, I want to send a List of User objects to the REST API.

Can the @RequestParam annotation support that?

For example:

@RequestMapping(method = RequestMethod.DELETE, value = "/delete")
    public @ResponseBody Integer delete(
            @RequestParam("users") List<Users> list) {
        Integer deleteCount = 0;
        for (User u : list) {
            if (u != null) {
                repo.delete(u);
                ++deleteCount;
            }
        }
        return deleteCount;
    }

In the MVC client, the url would be:

List list = new ArrayList<User>();
....
String url = "http://restapi/delete?users=" + list;

Solution

  • Request parameters are a Multimap of String to String. You cannot pass a complex object as request param.

    But if you just pass the username that should work - see how to capture multiple parameters using @RequestParam using spring mvc?

    @RequestParam("users") List<String> list

    But I think it would be better to just use the request body to pass information.