-1

I want to pass a list of objects in my request through @RequestParam but it doesn't work.
If I do :

 public List<Affaire> getAllAffaires(
                 @RequestParam(value = "consultantId", required = false) Long consultantId,
                 @RequestParam(value = "filtres", required = false) List<FilterDTO> filtres)

It doesnt go into the method, I get a 500 Error.
If I do :

 public List<Affaire> getAllAffaires(
                 @RequestParam(value = "consultantId", required = false) Long consultantId,
                 List<FilterDTO> filtres)

I am getting a :

Unable to evaluate the expression Method threw 'java.lang.IllegalArgumentException' exception.

If I pass another object which contains the list as :

 public List<Affaire> getAllAffaires(
                 @RequestParam(value = "consultantId", required = false) Long consultantId,
                 FilterDTOList filtres)

I get into the method but the property listFiltre of FilterDTOList is always null.

In Front Side (AngularJS) the call is :

function Affaire ($resource, DateUtils) {
        var resourceUrl =  'api/affaires/:id';

        return $resource(resourceUrl, {}, {
            'query': { method: 'GET', isArray: true,
                params:{
                    consultantId:'@consultantId',
                    filtres:'@filtres'
                },

How can I do pass my List as a parameter of my request?

thomas
  • 1,201
  • 2
  • 15
  • 35
  • User `@RequestBody FilterDTOList filtres` and pass filters in request body. – Sudhir Ojha Oct 25 '21 at 12:52
  • Could you provide more information? How are you doing the request to the service? Which HTTP verb are you using? – Nico Oct 25 '21 at 12:55
  • Can you provide stacktrace when 500 is thrown in the first example? It looks like there is no mapping defined for FilterDTO. Probably, if you inject request param to List and then map from String to FilterDTO, it will work but it's hard to tell without additional context – CaptainAye Oct 25 '21 at 13:00
  • there is not stacktrace , it seems that it doesnt even go into the java method. – thomas Oct 25 '21 at 13:07

2 Answers2

0

try with array instead of List and then convert that array to List

    @PostMapping(value = "/updateAllAffaires")
    @ResponseBody
    public List<FilterDTO> getSelectedGenes(@RequestParam(value = "consultantId", required = false) Long consultantId,
                                            @RequestBody FilterDTO[] filtersDto) {
        List<FilterDTO> filters = new ArrayList<>();
        // here you can use the logic to convert array to list
        return filters;
    }
Mani
  • 267
  • 2
  • 11
0

Try using @ModelAttribute or @Requestbody instead of @RequestParam.

public List<Affaire> getAllAffaires(
             @RequestParam(value = "consultantId", required = false) Long consultantId,
             @ModelAttribute(value = "filtres", required = false) List<FilterDTO> filtres)

You can read this answer for better understanding. This will also help.