I need to design a REST api ListMessages service (there are 4 fields in title, url, content and sender) that service should support two response versions within the same endpoint. The caller is able to define which response version he can handle.
Messages returned by the first version should contain only title, content and sender fields. The first version must not accept any other parameters than the version parameter.
Messages returned by the second version should return all 4 fields.The second version also takes a parameter which defines the format in which the response is returned (supported formats could be e.g. JSON and XML).
@RequestMapping(value ="/listMessages" , produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
@ResponseBody
public List<?> getAllMessages(@RequestParam Map<String,String> allParams){
if(allParams.get("version").equalsIgnoreCase("v1" ) && allParams.size()==1)
return listMessagesService.getAllMessages();
else if (allParams.get("version").equalsIgnoreCase("v2") && allParams.size()>1)
return listMessagesService.getAllMessagesV2("v2");
return null;
}
This is my code. Is the code acceptable and is it OK to design API like this?