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

Kafka Stream与Spring Cloud反序列化异常:求正确配置文件

解决java.lang.IllegalArgumentException: 类'[B'不在信任包列表问题

问题分析

报错里的[B是Java中byte[](字节数组)的类名简写,当前Json反序列化器的信任包列表未包含该类型,同时你的配置存在格式错误导致规则未生效:

  • 全局配置spring.kafka.consumer.properties.spring.json.trusted.packages=*:末尾多了冒号,导致配置无效
  • Spring Cloud Stream消费者仅指定业务包为信任范围,未覆盖byte[]所属的java.lang包,也未开启全局信任

正确配置文件

以下是合并并修正后的完整.yml配置:

server:
  port: 8787

spring:
  application:
    name: message-processor
  kafka:
    consumer:
      properties:
        # 开启全局信任所有包(消息来源可信时推荐)
        spring.json.trusted.packages: "*"
  cloud:
    function:
      definition: messageProcessor
    stream:
      bindings:
        messageProcessor-in-0:
          destination: first-topic
        messageProcessor-out-0:
          destination: first-topic.web-domains
      kafka:
        streams:
          binder:
            brokers:
              - localhost:9092
              - localhost:36161
              - localhost:40237 # 修正地址中的空格问题
        default:
          consumer:
            properties:
              # 针对Stream消费者同步信任规则
              spring.json.trusted.packages: "*"
        bindings:
          messageProcessor-in-0:
            consumer:
              configuration:
                key:
                  deserializer: org.apache.kafka.common.serialization.StringDeserializer
                value:
                  deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
          messageProcessor-out-0:
            producer:
              configuration:
                key:
                  serializer: org.apache.kafka.common.serialization.StringSerializer
                value:
                  serializer: org.springframework.kafka.support.serializer.JsonSerializer

关键修正说明

  1. 移除无效符号:删除全局配置末尾的冒号,确保信任规则生效
  2. 修正地址格式:去掉localhost :40237中的空格,避免连接失败
  3. 统一信任规则:在全局Kafka配置和Spring Cloud Stream默认消费者配置中,都设置spring.json.trusted.packages: "*",覆盖包括byte[]在内的所有类型
  4. 可选限制范围:如果不需要全局信任,可明确指定信任包,配置改为:spring.json.trusted.packages: java.lang, com.trisha.clients.collection, com.trisha.clients.collection.*(java.lang是byte[]所属包)

内容的提问来源于stack exchange,提问作者Nirav Kumar Patel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 00:55:17