Spring 自定义ClientHttpRequestInterceptor空响应

Spring 自定义ClientHttpRequestInterceptor空响应,spring,spring-boot,resttemplate,Spring,Spring Boot,Resttemplate,我有自己的clienthttpprequestinterceptor实现。在调用后的intercept方法中ClientHttpResponse response=execution.execute(请求,正文)我想处理响应, 但是当发生任何错误或超时时,变量response为null 然而,稍后当我捕获到restemplateexception时,我能够读取有关响应的一些信息,比如状态代码,甚至一些错误消息 有可能在ClientHttPrequestinInterceptor或任何其他拦截器中

我有自己的
clienthttpprequestinterceptor
实现。在调用后的
intercept
方法中
ClientHttpResponse response=execution.execute(请求,正文)
我想处理响应, 但是当发生任何错误或超时时,变量
response
null

然而,稍后当我捕获到
restemplate
exception时,我能够读取有关响应的一些信息,比如状态代码,甚至一些错误消息


有可能在ClientHttPrequestinInterceptor或任何其他拦截器中获取该信息吗?

有。如果我理解正确,您可以使用自己的
clienthttpprequestinterceptor
作为
restemplate
?如果是,只需添加自己的
ResponseErrorHandler实现即可:

...
var restTemplate = new RestTemplate();
restTemplate.setInterceptors(List.of(new OwnClientHttpRequestInterceptor()));
restTemplate.setErrorHandler(new OwnResponseErrorHandler());
...
在这种情况下,您可以在拦截器(例如,set header)中使用请求(通常用于请求,因为响应可以是未定义的)执行一些覆盖操作,并且有两种响应处理方法:在基本代码中获得响应后,或在错误处理程序中抛出错误时