java 为什么Web客户端中的ExchangeFilterFunction给出stackOverflowError?

vh0rcniy  于 2023-01-24  发布在  Java
关注(0)|答案(1)|浏览(161)

我已经附上了代码以及下面的stacktrace。我已经添加了ExchangeFilterFunction函数,用于在基于Spring Boot的项目中获取http statusCode值,并使用Webclient进行React式编程。我在加载时执行它时得到stackOverflowError。
请帮助说明为什么会发生这种情况。

public String publisher(String request, Map<String, Object> props) throws Exception {

        return webClientBuilder.filter(httpStatusCodePropsFilter(props)).build().post().uri(url).contentType(MediaType.APPLICATION_JSON)
                .body(Mono.just(request), String.class).retrieve()
                .onStatus(HttpStatus::isError, response -> response.bodyToMono(String.class)
                        .flatMap(error -> Mono.error(new Exception(error))))
                .bodyToMono(String.class).block();

    }
public ExchangeFilterFunction httpStatusCodePropsFilter(Map<String,Object> prop) {
        try {

      return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
        if (clientResponse.statusCode() != null) {
          prop.put(Constants.HTTP_RESPONSE_CODE, clientResponse.statusCode().value());
          return Mono.just(clientResponse);
        } else {
          return Mono.just(clientResponse);
        }
      });
    } catch (Exception exception) {
      e.printstacktrace();
      throw exception;
    }
 }
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1079)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:286)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:260)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:137)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:350)
    at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at weblogic.websocket.tyrus.TyrusServletFilter.doFilter(TyrusServletFilter.java:274)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:96)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:126)
    at org.springframework.boot.web.servlet.support.ErrorPageFilter.access$000(ErrorPageFilter.java:64)
    at org.springframework.boot.web.servlet.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:101)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:119)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3709)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3675)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:344)
    at weblogic.security.service.SecurityManager.runAsForUserCode(SecurityManager.java:197)
    at weblogic.servlet.provider.WlsSecurityProvider.runAsForUserCode(WlsSecurityProvider.java:203)
    at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:71)
    at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2446)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2294)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2272)
    at weblogic.servlet.internal.ServletRequestImpl.runInternal(ServletRequestImpl.java:1705)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1665)
    at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:272)
    at weblogic.invocation.ComponentInvocationContextManager._runAs(ComponentInvocationContextManager.java:352)
    at weblogic.invocation.ComponentInvocationContextManager.runAs(ComponentInvocationContextManager.java:337)
    at weblogic.work.LivePartitionUtility.doRunWorkUnderContext(LivePartitionUtility.java:57)
    at weblogic.work.PartitionUtility.runWorkUnderContext(PartitionUtility.java:41)
    at weblogic.work.SelfTuningWorkManagerImpl.runWorkUnderContext(SelfTuningWorkManagerImpl.java:652)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:420)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:360)
Caused by: java.lang.StackOverflowError
    at reactor.netty.transport.Transport.<init>(Transport.java:50)
    at reactor.netty.transport.ClientTransport.<init>(ClientTransport.java:42)
    at reactor.netty.http.client.HttpClient.<init>(HttpClient.java:102)
    at reactor.netty.http.client.HttpClientConnect.<init>(HttpClientConnect.java:89)
    at reactor.netty.http.client.HttpClientFinalizer.<init>(HttpClientFinalizer.java:48)
    at reactor.netty.http.client.HttpClient.request(HttpClient.java:1307)
    at org.springframework.http.client.reactive.ReactorClientHttpConnector.connect(ReactorClientHttpConnector.java:106)
    at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.exchange(ExchangeFunctions.java:103)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$ofResponseProcessor$4(ExchangeFilterFunction.java:97)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$null$0(ExchangeFilterFunction.java:64)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$ofResponseProcessor$4(ExchangeFilterFunction.java:97)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$null$0(ExchangeFilterFunction.java:64)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$ofResponseProcessor$4(ExchangeFilterFunction.java:97)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$null$0(ExchangeFilterFunction.java:64)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$ofResponseProcessor$4(ExchangeFilterFunction.java:97)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$null$0(ExchangeFilterFunction.java:64)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$ofResponseProcessor$4(ExchangeFilterFunction.java:97)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$null$0(ExchangeFilterFunction.java:64)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$ofResponseProcessor$4(ExchangeFilterFunction.java:97)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$null$0(ExchangeFilterFunction.java:64)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$ofResponseProcessor$4(ExchangeFilterFunction.java:97)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$null$0(ExchangeFilterFunction.java:64)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$ofResponseProcessor$4(ExchangeFilterFunction.java:97)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$null$0(ExchangeFilterFunction.java:64)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$ofResponseProcessor$4(ExchangeFilterFunction.java:97)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$null$0(ExchangeFilterFunction.java:64)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$ofResponseProcessor$4(ExchangeFilterFunction.java:97)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$null$0(ExchangeFilterFunction.java:64)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$ofResponseProcessor$4(ExchangeFilterFunction.java:97)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$null$0(ExchangeFilterFunction.java:64)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$ofResponseProcessor$4(ExchangeFilterFunction.java:97)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$null$0(ExchangeFilterFunction.java:64)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$ofResponseProcessor$4(ExchangeFilterFunction.java:97)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$null$0(ExchangeFilterFunction.java:64)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$ofResponseProcessor$4(ExchangeFilterFunction.java:97)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$null$0(ExchangeFilterFunction.java:64)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$ofResponseProcessor$4(ExchangeFilterFunction.java:97)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$null$0(ExchangeFilterFunction.java:64)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$ofResponseProcessor$4(ExchangeFilterFunction.java:97)
    at org.springframework.web.reactive.function.client.ExchangeFilterFunction.lambda$null$0(ExchangeFilterFunction.java:64)
5jvtdoz2

5jvtdoz21#

我在ExchangeFilterFunction上遇到了同样的递归错误。我使用的想法和你的一样(使用ExchangeFilterFunction处理特定的client4xx或server5xx错误)我认为发生的情况是,每次调用方法时,ExchangeFilterFunction都被创建并添加到WebClient的同一个示例中。您的错误处理程序将有500个ExchangeFilterFunction示例,因此,在返回实际响应之前,您的错误将被处理500x。要更正此问题,您需要确保在您的WebClient中只存在一个ExchangeFilterFunction来处理特定的client4xx或server5xx。

public Service PublisherService(){
   private WebClient webClient;
   public PublisherService(){
      this.webClient = webclientInitiator();
   }
   private WebClient webclientInitiator(){ 
      return webClientBuilder.filter(httpStatusCodePropsFilter(props)).build();
   } 
   public String publisher(String request, Map<String, Object> props) throws Exception {

        return webClient.post().uri(url).contentType(MediaType.APPLICATION_JSON)
                .body(Mono.just(request), String.class).retrieve()
                .onStatus(HttpStatus::isError, response -> response.bodyToMono(String.class)
                        .flatMap(error -> Mono.error(new Exception(error))))
                .bodyToMono(String.class).block();

    }
    public ExchangeFilterFunction httpStatusCodePropsFilter(Map<String,Object> prop) {
        try {

      return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
        if (clientResponse.statusCode() != null) {
          prop.put(Constants.HTTP_RESPONSE_CODE, clientResponse.statusCode().value());
          return Mono.just(clientResponse);
        } else {
          return Mono.just(clientResponse);
        }
      });
    } catch (Exception exception) {
      e.printstacktrace();
      throw exception;
    }
 }

}

这样,ExchangeFilterFunction只添加一次。希望这能回答您的问题。

相关问题