Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用HttpEntity在Spring RestTemplate中删除<;列表>;_Spring_Spring Boot_Resttemplate_Spring Rest - Fatal编程技术网

使用HttpEntity在Spring RestTemplate中删除<;列表>;

使用HttpEntity在Spring RestTemplate中删除<;列表>;,spring,spring-boot,resttemplate,spring-rest,Spring,Spring Boot,Resttemplate,Spring Rest,我不知道为什么我的代码不起作用,我尝试过邮递员,效果很好: 但是使用restemplate时,当它使用相同的端点时,我无法得到响应 ResponseEntity<String> responseMS = template.exchange(notificationRestService, HttpMethod.DELETE, new HttpEntity<NotificationRestDTO[]>(arrNotif), String.class); 有什么帮助吗?

我不知道为什么我的代码不起作用,我尝试过邮递员,效果很好:

但是使用
restemplate
时,当它使用相同的端点时,我无法得到响应

ResponseEntity<String> responseMS  = template.exchange(notificationRestService, HttpMethod.DELETE, new HttpEntity<NotificationRestDTO[]>(arrNotif), String.class);

有什么帮助吗??谢谢

从评论中可以清楚地看出,您希望它返回400个错误请求响应
RestTemplate
将这些视为“客户端错误”,并将抛出一个错误

如果要处理这样的情况,应捕获此异常,例如:

try {
    ResponseEntity<String> responseMS  = template.exchange(notificationRestService, HttpMethod.DELETE, new HttpEntity<NotificationRestDTO[]>(arrNotif), String.class);
} catch (HttpClientErrorException ex) {
    String message = ex.getResponseBodyAsString();
}
试试看{
ResponseEntity responseMS=template.exchange(notificationRestService,HttpMethod.DELETE,新的HttpEntity(arrNotif),String.class);
}捕获(HttpClientErrorException-ex){
字符串消息=例如getResponseBodyAsString();
}
在这种情况下(因为您需要
字符串
),可以使用该方法



ResponseEntity
仅在您的请求能够成功执行的情况下包含数据(2xx状态代码,如200204,…)。因此,如果您只希望在请求未成功的情况下返回消息,那么您实际上可以按照Mouad在评论中提到的操作,并且您可以使用
RestTemplate
delete()
方法,您也应该使用
delete
作为RestTemplate的http方法?类似于:
new RestTemplate().delete(uri,params)
@MouadELFakir
restemplate.exchange()
也可以使用。据我所知,
delete()
API不允许您发送请求正文。米格尔,您能告诉我们调用
restemplate.exchange()
的结果吗。它会抛出异常还是其他什么?@g00glen00b我从SoapUI中只得到一个400 null,在Spring类的代码中,我看不到任何关于你答案的线索!正如更新
testrestemplate.delete(…)
仍然是
void
。。。我不明白为什么没有
deleteForEntity
至于所有其他的httpmethod,仅仅是
response.body=null
实际上,对于
restemplate.patch(…)
也是如此
try {
    ResponseEntity<String> responseMS  = template.exchange(notificationRestService, HttpMethod.DELETE, new HttpEntity<NotificationRestDTO[]>(arrNotif), String.class);
} catch (HttpClientErrorException ex) {
    String message = ex.getResponseBodyAsString();
}