You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3中HttpExchange客户端调试时抛出IllegalStateException问题

调试HttpExchange客户端代理时出现toString()异常问题

依赖版本

  • Spring Boot 3.0.4
  • Spring WebFlux 6.0.6

代码实现

@HttpExchange("${path.to.baseUrl}")
public interface ExternalClient {

   @PostExchange("${path.to.endpoint}")
   Mono<Object> post(@RequestBody Object object);

}

@Bean
public ExternalClient externalClient(HttpServiceProxyFactory httpServiceProxyFactory) {
    return httpServiceProxyFactory.createClient(ExternalClient.class);
}

@Bean
public HttpServiceProxyFactory httpServiceProxyFactory(WebClient webClient) {
    return HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient))
            .embeddedValueResolver(configurableBeanFactory::resolveEmbeddedValue)
            .build();
}

问题描述

externalClient Bean运行正常,但在调试模式下查看该对象时,抛出异常:

Method threw 'java.lang.IllegalStateException' exception. Cannot evaluate jdk.proxy2.$Proxy134.toString()

解决方案

方法一:在接口中显式声明toString()方法

在ExternalClient接口中添加toString()的重写声明,让JDK代理类正确实现该方法:

@HttpExchange("${path.to.baseUrl}")
public interface ExternalClient {

   @PostExchange("${path.to.endpoint}")
   Mono<Object> post(@RequestBody Object object);

   @Override
   String toString();
}

方法二:调试时规避直接调用toString()

调试过程中,不要直接查看代理对象的toString结果,转而查看代理的内部属性(如h字段对应的InvocationHandler),或者通过日志输出代理对象的关键信息。

原因说明

JDK动态代理基于接口生成,只有当接口中显式声明了toString()、hashCode()、equals()这些Object类的方法时,代理类才会将这些方法的调用委托给InvocationHandler处理。如果接口未声明这些方法,调试时IDE尝试调用toString()会触发代理类的默认处理逻辑,进而抛出IllegalStateException。

内容的提问来源于stack exchange,提问作者tliub666

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 12:42:53