I tried invoking delete operation via RestTemplate's delete method but it's not working. My Rest controller code for delete
@RequestMapping(method = RequestMethod.DELETE)
@ResponseBody
public DeleteResponse deleteRecords(
@RequestBody final DeleteRequest DeleteRequest) {
DeleteResponse response=new DeleteResponse();
//invoke respective services to do delete operations
response.setStatus("success");
response.setMessage("all records deleted successfully");
return response;
}
Class DeleteRequest{
@JsonProperty(value="userId")
String userId;
@JsonProperty(value="records")
List<Record> records;
}
class Record{
@JsonProperty(value="key1")
String key1;
@JsonProperty(value="key2")
String key2;
@JsonProperty(value="key3")
String key3;
}
//Client invocation
public void deleteRecords(String url,DeleteRequest deleteRequest){
resttemplate.delete(url, deleteRequest);
}
I am invoking put in similar way its working fine. for delete i get below exception Exception occured : Could not read JSON: No content to map due to end-of-input.
To check whether there is a problem in the way I am passing the json values I created a class extending RestTemplate and added a method performActionForObject
public class MyRestTemplate extends RestTemplate {
public <T> T performActionForObject(String url, Object request, Class<T> responseType,HttpMethod method) throws RestClientException {
RequestCallback requestCallback = httpEntityCallback(request, responseType);
HttpMessageConverterExtractor<T> responseExtractor =
new HttpMessageConverterExtractor<T>(responseType, getMessageConverters());
return execute(url, method, requestCallback, responseExtractor);
}
}
then invoked delete operation as below
DeleteResponse response= resttemplate.performActionForObject(url, deleteRequest, DeleteResponse.class, HttpMethod.DELETE);
It didn't work i got the same exception so i changed delete to post in both controller and rest client it worked perfectly.
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public DeleteResponse deleteRecords(
@RequestBody final DeleteRequest DeleteRequest) {
DeleteResponse response= resttemplate.performActionForObject(url, deleteRequest, DeleteResponse.class, HttpMethod.POST);
Not sure why it's not working when i use delete