通过 Spring RestTemplate 调用带请求体的 Delete 方法(Delete With Request Body)
Spring 框架的RestTemplate 类定义了一些我们在通过 java 代码调用 Rest 服务时经常需要用到的方法,使得我们通过 java 调用 rest 服务时更加方便、简单。但是 RestTemplate 的 delete 方法并不支持传入请求体(Request Body)。经测试,通过调用 RestTemplate 类的exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<ResponseResult> responseType, Object... uriVariables) 方法,将 method 指定为 org.springframework.http.HttpMethod.DELETE,并传入 requestEntity(请求体) 对象时,在服务端得到的 Request Body 仍然为 null。可见 RestTemplate 默认并不支持对 DELETE 方法使用请求体。
通过查阅资料发现 RestTemplate 默认是使用 spring 自身的 SimpleClientHttpRequestFactory 创建请求对象和对其进行相关设置(如请求头、请求体等),它只支持 PUT 和 POST 方法带请求体,RestTemplate 的 DELETE 方法不支持传入请求体是因为 JDK 中 HttpURLConnection 对象的 delete 方法不支持传入请求体(如果对 HttpURLConnection 对象的 delete 方法传入请求体,在运行时会抛出 IOException)。
我们可以通过修改 RestTemplate 的 RequestFactory 实现 delete 方法对请求体的支持,比如改成 Apache HTTPClient’s HttpComponents 的ClientHttpRequestFactory,由于 ClientHttpRequestFactory 默认的 DELETE 方法也不支持请求体,所以我们除了修改 RestTemplate 的 RequestFactory 之外,还需要定义一个支持传输请求的的 DELETE 请求模型,完整的代码如下:
RestTemplate restTemplate = new RestTemplate();
public static class HttpEntityEnclosingDeleteRequest extends HttpEntityEnclosingRequestBase {
public HttpEntityEnclosingDeleteRequest(final URI uri) {
super();
setURI(uri);
}
@Override
public String getMethod() {
return "DELETE";
}
}
@Test
public void bodyWithDeleteRequest() throws Exception {
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory() {
@Override
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
if (HttpMethod.DELETE == httpMethod) {
return new HttpEntityEnclosingDeleteRequest(uri);
}
return super.createHttpUriRequest(httpMethod, uri);
}
});
ResponseEntity<String> exchange = restTemplate.exchange(
"http://example.com/file";,
HttpMethod.DELETE,
new HttpEntity<String>("some sample body sent along the DELETE request"),
String.class);
assertEquals("Got body: some sample body sent along the DELETE request", exchange.getBody());
}
服务端的 java 代码:
@RequestMapping(value = "/file", method = DELETE)
@ResponseBody
public String deleteSecci(@RequestBody String body) {
return "Got body: " + body;
}
通过 Spring RestTemplate 调用带请求体的 Delete 方法(Delete With Request Body)的更多相关文章
- Spring RestTemplate 之get请求
一,简介:Spring RestTemplate 是 Spring 提供的用于访问 Rest 服务的客户端,RestTemplate 提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写 ...
- Spring RestTemplate 之post请求
●post请求:在RestTemplate中,POST请求可以通过如下三个方法来发起,但post提交方式又有两种 formData 和 payLoad,而且接口设计与传统的浏览器使用的提交方式又有差异 ...
- 使用 Spring RestTemplate 调用 rest 服务时自定义请求头(custom HTTP headers)
在 Spring 3.0 中可以通过 HttpEntity 对象自定义请求头信息,如: private static final String APPLICATION_PDF = "app ...
- spring restTemplate 进行http请求的工具类封装
本文为博主原创,未经允许不得转载: 1.对常用调用的方法进行封装: import org.springframework.http.HttpHeaders; import com.alibaba.fa ...
- Spring RestTemplate get post 请求 携带 headers
RestTemplate 1.我用RestTemplate请求时 我把他注入到容器里 这样可以 什么用什么时候拿 2.也可以new出来 不过我不喜欢 所以就没有用new的 下面我自己的方法 先注 ...
- 定时器(setTimeout/setInterval)调用带参函数失效解决方法
也许你曾碰到过这样的问题,不管是setInterval()还是setTimeout(),当code参数里放一个带参函数时,定时器都会失效,看下面这个例子: function test(str){ al ...
- 定时器(setTimeout和setInterval)调用带参函数失效解决方法
方法1:用匿名函数包裹 function test(str){ alert(str); } var a = "abcde" setTimeout(function(){ ...
- setTimeout调用带参数的函数的方法
function test(s){ alert(s);}window.setTimeout(function(){test('str');},1000);这样就可以了...为什么是这样呢.因为s ...
- Spring RestTemplate 小结
关于RestTemplate 首先,你可以把它理解为一个发起请求并接收响应的工具类(功能类似浏览器). 其次,它其实是一个壳,具体还是通过调用别的接口来实现(如jdk自带的连接,或者HttpClien ...
随机推荐
- python none,null,,,,,类型
内建类型None表示一个空对象,没有方法和属性. None是一个特殊的常量. None和False不同. None不是0. None不是空字符串. None和任何其他的数据类型比较永远返回False. ...
- Sicily-1006
一. 题意 这道题就是考排列组合吧,再来就是比较一下字符的下标算一下两个ranking的距离.然后我总结了一个排列和一个组合的实现方法,这道题直接用的是stl 里面的next_permutation ...
- PHP中magic_quotes_gpc和 magic_quotes_runtime区别及其反斜线转义问题
php中关于反斜线转义: php中数据的魔法引用函数 magic_quotes_gpc 或 magic_quotes_runtime 设置为on时,当数据遇到 单引号' 和 双引号&quo ...
- Phonegap-----Media
Everything in the code: <!DOCTYPE html> <html> <head> <title>Media Example&l ...
- K - K.Bro Sorting
Description Matt’s friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubb ...
- MFC工程的复制
MFC工程的复制 [1] 在VS中新建一个同类型的MFC工程. [2] 复制.rc资源文件,用记事本打开旧工程和新工程的.rc文件,将旧工程的对应部分复制到新工程的对应部分,文 ...
- iOS Development: Proper Use of initWithNibName:bundle: Affects UITableViewController
Address:http://www.outofcore.com/2011/07/ios-development-proper-use-of-initwithnibnamebundle-affects ...
- [Swust OJ 85]--单向公路(BFS)
题目链接:http://acm.swust.edu.cn/problem/0085/ Time limit(ms): 5000 Memory limit(kb): 65535 Descriptio ...
- javascript 简单实现对两个数组相似度的检验
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- RAR压缩解压命令
RAR压缩解压命令 这几天一直没空更新博客,现在补上: 先介绍一下rar的命令格式及相关参数含义(摘自rar): 用法: rar <命令> -<开关 1> -<开关 ...