How to pass array as PathVariable in spring rest template?




Asked on July 11, 2018
Hello,

I want to pass array as path parameter in URL. My code is as follows:
Controller:
@RequestMapping(value = "/getAllProcessInstancesByLocation/[{location_id}]" , method = RequestMethod.GET)
     public String getAllProcessInstances(@PathVariable("location_id") String[] location_id) {
         try {
        return processService.getAllProcessInstancesByLocation(location_id);
         }catch(Exception e) {
             return "error=>"+e.getMessage();
         }
         
     }

When I try to test this through browser I am getting following error:


http://localhost:8088/super-admin/getAllProcessInstancesByLocation/[%7B8,9%7D]



Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Jul 11 12:54:55 IST 2018
There was an unexpected error (type=Internal Server Error, status=500).
Could not get HttpServletRequest URI: Illegal character in path at index 67: http://localhost:8088/super-admin/getAllProcessInstancesByLocation/[%7B8,9%7D]

I want to pass the array in url. Means I want to pass as follows:
http://localhost:8088/super-admin/getAllProcessInstancesByLocation/[7,9,11]


Can any one please provide solution?

Thanks & Regards
Shilpa V Kulkarni




Replied on July 11, 2018
Remove [ ] from method and URL both and try

Method:

@RequestMapping(value = "/getAllProcessInstancesByLocation/{location_id}" , method = RequestMethod.GET)
public String getAllProcessInstances(@PathVariable("location_id") String[] location_id) {
    try {
        return processService.getAllProcessInstancesByLocation(location_id);
    }catch(Exception e) {
        return "error=>"+e.getMessage();
    }
    
}


URL:

http://localhost:8088/super-admin/getAllProcessInstancesByLocation/7,9,11


Write Answer











©2024 concretepage.com | Privacy Policy | Contact Us