Reputation: 1179
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
Upvotes: 0
Views: 5288
Reputation: 1956
You pass JSON like that. ArrayList does not accept key and value pair. Then add your keys
JSON:
[
"1",
"2",
"3",
"4"
]
@RequestMapping(method = RequestMethod.POST, value = "/read/ids/",headers="Accept=application/json")
public ArrayList<String> test(@RequestBody ArrayList<String> ids) throws Exception {
return ids;
}
Upvotes: 1