Search code examples
javaangularquery-stringhttp-parameters

How to add parameters in service call for @RequestParam backend?


If i have something like

@GetMapping(value = "/list")
public ResponseEntity<MyDTO> findStatusPerEC(@RequestParam final List<Long> numbersEC) 

How do I add numbersEC in my url on the frontend? Is this query parameter?

I know this one was for old call that didn´t had query parameters and data was only a number (long)

return this.http.get<any>(URL_API + '/simulator/status/' + data);

But now I have to send a list of long values...may you help me?


Solution

  • return this.http.get<any>(URL_API + '/simulator/status/' + data);
    

    Since you mentioned data is only a long type, what you are referring to here when you make the above request is a PathVariable it is slightly different to a RequestParam.

    • Path variables have the syntax: /simulator/status/:statusID where statusID is dynamic and extracts values from the URI.
    • Request parameters have the syntax: ?arg=val&arg2=val2 etc... and extract values from the request query string.

    Solution

    To answer your question, to send an array across as request parameters, you can do it like so:

    ?myparam=myValue1&myparam=myValue2&myparam=myValue3

    As you can see, above myparam is unchanging, and the values are variable, hence the data within your list data structure.

    So when you're making your request it will look similar to this:

    Angular/Javascript

    return this.http.get<any>(URL_API + '/list' + '?myparam=myValue1&myparam=myValue2&myparam=myValue3');
    

    Java

    @GetMapping(value = "/list")
    public ResponseEntity<MyDTO> findStatusPerEC(@RequestParam final List<Long> numbersEC) 
    

    I hope this helps.