简体   繁体   中英

How to pass generic List<User> to restful web services

I have the requirement to create the search operation using restful web services, ie, using @GET. The method signature takes String and List as input argument and returns List.

public Generic List <Employer> getAllEmployer(String employeeName, Generic List <employeeLocation>);

Kindly request if someone could describe on how to implement the same. Should I use query param or path param or form param. I need to return the List of employer in json format.

if the employee locations are just string, pass them as comma seperated values and spring will take care of converting it to list. I would rather suggest just to have it as a pathparam rather than having it as a query param.

This is just my opinion but I think that the "RESTful" way to pass multiple parameters with the same name would be to a MultiValueMap.

Spring and Jersey both have implementations of MultiValueMap however, below is an example of a spring implementation:

@RequestMapping(method = RequestMethod.GET, value = {"/employer/_search"})
public List<Employer> search(@RequestParam MultiValueMap<String,String> params) {
    return someService.search(params);
}

They way that you would call this url would then become:

/employer/_search?employeeName=name&location=1&location=2&location=3

Then behind the scenes spring will create the MultiValueMap for you which is a Map<String,List<String>> where any parameters with the same name are put into the same list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM