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
相关产品推荐
相关产品推荐

