休息模板删除不起作用

时间:2015-09-03 08:38:44

标签: spring-mvc resttemplate http-method

我尝试通过RestTemplate的删除方法调用删除操作,但它无法正常工作。 我的Rest控制器代码用于删除

@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;
  }

//客户端调用

 public void deleteRecords(String url,DeleteRequest deleteRequest){
    resttemplate.delete(url, deleteRequest);
}

我以类似的方式调用 put 工作正常。 删除我得到以下异常 发生异常:无法读取JSON:由于输入结束而没有要映射的内容。

为了检查传递json值的方式是否存在问题,我创建了一个扩展RestTemplate的类并添加了一个方法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);
    }

}

然后调用删除操作,如下所示

DeleteResponse response= resttemplate.performActionForObject(url, deleteRequest, DeleteResponse.class, HttpMethod.DELETE);

它没有用,我得到了同样的例外 所以我改变删除发布在控制器和休息客户端它工作 完美。

 @RequestMapping(method = RequestMethod.POST)
     @ResponseBody
      public DeleteResponse deleteRecords(
      @RequestBody final DeleteRequest DeleteRequest) {

DeleteResponse response= resttemplate.performActionForObject(url, deleteRequest, DeleteResponse.class, HttpMethod.POST);

当我使用删除

时,不确定为什么它不起作用

0 个答案:

没有答案