Home > OS >  Spring Rest : pass an object in RequestParam
Spring Rest : pass an object in RequestParam

Time:10-27

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?

CodePudding user response:

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;
    }

CodePudding user response:

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.

  • Related