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

如何在Spring Cloud Gateway中用YAML正确配置ModifyRequestBody过滤器?

解决Spring Cloud Gateway YAML配置ModifyRequestBody过滤器的ConverterNotFoundException问题

问题根源

你遇到的ConverterNotFoundException是因为YAML配置中直接用字符串指定inClass和outClass时,Spring无法将字符串转换为过滤器所需的ParameterizedTypeReference类型,而编程式配置中直接传入Class对象则能被正确识别。

正确的YAML配置

修改inClass和outClass的值,使用Spring SpEL的T()表达式来指定类型:

spring:
  cloud:
    gateway:
      routes:
        - id: microservice
          uri: http://localhost:8080
          predicates:
            - Path=/books
          filters:
            - name: ModifyRequestBody
              args:
                rewriteFunction: "#{@myRewriteFunction}"
                inClass: T(java.lang.String)
                outClass: T(java.lang.String)

关键说明

  • T(fully.qualified.class.name)是SpEL语法,用于获取指定类的Class对象,这样过滤器工厂就能正确将其转换为所需的ParameterizedTypeReference。
  • 你的自定义MyRewriteFunction已经正确实现RewriteFunction<String, String>并通过@Component注册为Spring Bean,#{@myRewriteFunction}的引用方式是正确的。
  • 如果需要处理复杂自定义对象,同样用T()表达式指定全类名即可,例如T(com.example.dto.MyRequestDto)。

验证

修改配置后重启服务,发送请求到/books路径,过滤器即可正常调用自定义重写函数,不再抛出转换异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 00:58:15