使用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)
@MouadELFakirrestemplate.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();
}