Spring Boot: Is it possible to send List of Objects as QueryParameter?

Viewed 693

How to send the List of Objects as QueryParameter ??? Is it possible to send objects as QueryParameter?

User u1=new User();
User u2=new User();

List<User> user=new ArrayList<>();
user.add(u1);
user.add(u2);

// How to send the above List of Objects as QueryParameter ???

HttpEntity<Object> entity=new HttpEntity<>(user,headers);

ResponseEntity<boolean> response= restTemplate.postForEntity(url,entity,User.class);

Also how to receive the list of objects response ??

boolean getResponse( @RequestParam List<User> user ){  // This way is not possible.
}
1 Answers

One method would be to use a wrapper object to hold the list of objects

class UserDto{
    private List<User> persons;

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }   
}

//controller
@RestController
@RequestMapping(value="user/save")
public @ResponseBody UserDto saveUsers(@RequestBody UserDto users,
   method=RequestMethod.POST){
   //access users object which is has the list of users here
} 

Using a wrapper makes it convenient to sent a response back as an object while using a RestTemplate.

restTemplate.postForEntity(url,entity,UserDto.class)

Otherwise, you could also send and array of User object

restTemplate.postForEntity(url,entity,User[].class)
Related