I am new to rest webservice. Could some one please help me to identify the correct approach.
I want My webservice to be consumed Json object ( List id) and produces the Json object.
I want to pass given json data to my service.
{
id : "1"
id : "2"
id : "3"
id : "4"
}
and My service is looking like -
@RequestMapping(value = "/read/ids", method = RequestMethod.POST)
@ResponseBody
public List<ids> getValues(Collection<String> ids){
// some calculation....
}
This code is not working for me. I am using spring framework to create my service.
alernate way that is working for me is -
pass the simple data
1,2
and then service to consume it.
@RequestMapping(value = "/read/ids", method = RequestMethod.POST)
@ResponseBody
public List<String> getValues(@Valid @RequestBody String ids){
List<String> testids = new ArrayList<>();
if(ids.contains(",")){
String[] idArray = ids.split(",");
for(String id : idArray){
testids.add(service.findByid(id));
}
}else{
testids.add(service.findByid(ids));
}
return testids;
}
BUt I don;t think its correct approach
Note : I don't want to create unnecessary object that would contain list