Вопрос

I am trying to use DELETE method of HttpMethod. The code that I am using for that is

response = restTemplate.exchange(url, HttpMethod.DELETE, requestEntity, Response.class);

I am also using JacksonJson for mapping json. The delete functionality returns the json which should be mapped to Response class. But calling the above line doesn't works and gives internal server error with 500 as response code. But, the same API does work with RESTClient in the browser so I guess there is something that I am not doing correctly.

Это было полезно?

Решение

After doing some more research it seems that DELETE method doesn't supports request body. As we had the control over REST API we have changed the request body to be added as parameters. After doing this change the request is working properly.

Hope it helps someone.

Другие советы

A little late to the party I'd like to chime in here as well (document my solution for posterity)

I'm too using spring's rest template, also trying to perform a delete request with a payload AND i'd also like to be able to get the response code from the server side

Disclaimer: I'm on Java 7

My solution is also based on a post here on SO, basically you initially declare a POST request and add a http header to override the request method:

RestTemplate tpl = new RestTemplate();

/*
 * http://bugs.java.com/view_bug.do?bug_id=7157360
 * As long as we are using java 7 we cannot expect output for delete
 * */
HttpHeaders headers = new HttpHeaders();
headers.add("X-HTTP-Method-Override", "DELETE");

HttpEntity<Collection<String>> request = new HttpEntity<Collection<String>>(payload, headers);
ResponseEntity<String> exchange = tpl.exchange(uri, HttpMethod.POST, request, String.class);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top