java - 在Spring中使用ClientHttpRequestInterceptor时如何中止请求?

标签 java spring rest

如果您阅读 javadocClientHttpRequestInterceptor.intercept 方法上,它说:

execute the request using ClientHttpRequestExecution.execute(org.springframework.http.HttpRequest, byte[]), or do not execute the request to block the execution altogether.

该语句的第二部分是我想要实现的目标(根据某些标准完全阻止执行)。然而,我这样做并没有成功。我已经做了几个小时的研究,但仍然找不到任何关于如何完成此操作的示例。

我尝试返回 null,后来在流程中得到了 NPE,因为 Spring 尝试从响应中获取 statusCode(下面的异常(exception))。

java.lang.NullPointerException: null
    at org.springframework.web.client.DefaultResponseErrorHandler.getHttpStatusCode(DefaultResponseErrorHandler.java:56)
    at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:50)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:655)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:620)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:588)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:536)

编辑:这是一个简单的拦截器,供我尝试实现的目标引用:

  public class FailSafeInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
      try{
        return execution.execute(request, body);
      }catch(Throwable e){
        return null;
      }
    }
  }

可能的解决方案:在解决这个问题之后,我设法使用以下代码(使用 MockResponse)使其工作。但我不确定这是否是文档所指的要使用的方法的方式。如果有更好的方法来做到这一点,我将不胜感激。

  public class FailSafeInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
      try{
        return execution.execute(request, body);
      }catch(Throwable e){
        return new MockClientHttpResponse(new byte[]{}, HttpStatus.OK);
      }
    }
  }

最佳答案

MockClientHttpResponse 来自 spring-test ,在生产代码中包含测试类很尴尬。

如果您想在检测到某些内容后中止发送整个请求,只需抛出 RuntimeException (或使用内置 RestClientException):

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {

        if(checkNeedAbortSendingRequst()){
           throw new RestClientException("Bla blab bla");
        }
        return execution.execute(request, body);   
    }
<小时/>

更新:

如果您想安全失败,您当前的解决方案看起来不错,希望在捕获异常时我会使用非 200 状态代码(可能是 500?)。由于我也无法在 spring-mvc 中找到可以从头开始创建或没有任何外部依赖项的 ClientHttpResponse 实现,因此我会从以下位置复制 MockClientHttpResponse spring-test 到要使用的项目。

关于java - 在Spring中使用ClientHttpRequestInterceptor时如何中止请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58944422/

相关文章:

spring - 是否有提供者不可知的方式来获取 Spring 框架中的最新缓存统计信息?

java - 使用 JavaParser 更改方法级别的字符串变量

java - 半透明的内部框架-这在java中可能吗?

Spring @Autowire 多个对象

rest - WSO2 BAM:如何通过REST端点发送日志事件

asp.net-mvc - 以 Web 服务为模型的 ASP.Net MVC?

jquery - 不允许通过 REST api 和 jquery 405 方法创建 Jira 问题

Java ClassLoader委托(delegate)模型?

java - 在Android中创建应用程序时如何只创建一次变量?

java.lang.LinkageError : loader constraint violation for Spring MVC and Thymeleaf