I'm facing a ClassCastException with Spring Cloud Bus when consuming EnvironmentBusRemoteApplicationEvent (triggered via /busenv endpoint) from a Kafka topic. The issue only occurs when I configure a named group.id on the consumer. With the default anonymous consumer group, everything works perfectly.
Environment
Spring Boot: 3.3.10
Spring Cloud Bus: 4.1.2
Apache Kafka (with SSL)
Configuration
Here's the relevant part of my application.yaml:
yaml
spring:
cloud:
bus:
enabled: true
destination: XPTO.bus.2t
id: xpto-api-stable:2t
refresh.enabled: true
env.enabled: true
trace.enabled: true
stream:
bindings:
springCloudBusInput:
destination: XPTO.bus.2t
group: ${HOSTNAME} # Named group = POD_NAME (e.g., xpto-api-stable-7957c686d8-hfgz8)
binder: kafka
springCloudBusOutput:
binder: kafka
kafka:
binder:
brokers: ${KAFKA_BOOTSTRAP_SERVERS}
configuration:
client.id: ${spring.application.name}-${HOSTNAME}
auto.offset.reset: latest
security.protocol: SSL
# ... SSL configuration omitted for brevity
The Problem
When group: ${HOSTNAME} is set (a named consumer group), consuming an EnvironmentBusRemoteApplicationEvent (triggered by POST /busenv) fails with:
text
java.lang.ClassCastException: class [B cannot be cast to class org.springframework.cloud.bus.event.RemoteApplicationEvent
at org.springframework.cloud.bus.BusConsumer.accept(BusConsumer.java:31)
Full stack trace:
text
Caused by: java.lang.ClassCastException: class [B cannot be cast to class org.springframework.cloud.bus.event.RemoteApplicationEvent ([B is in module java.base of loader 'bootstrap'; org.springframework.cloud.bus.event.RemoteApplicationEvent is in unnamed module of loader org.springframework.boot.loader.launch.LaunchedClassLoader @5f282abb)
at org.springframework.cloud.bus.BusConsumer.accept(BusConsumer.java:31)
at org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry$FunctionInvocationWrapper.invokeConsumer(SimpleFunctionRegistry.java:1063)
... 37 more
The message payload arrives as byte[224] instead of being deserialized into RemoteApplicationEvent.
What Works
- Anonymous consumer group – If I remove the
groupproperty, the error disappears and/busenvworks correctly:
yaml
spring:
cloud:
stream:
bindings:
springCloudBusInput:
destination: XPTO.bus.2t
# group: ${HOSTNAME} # <-- commented out = anonymous group works fine
The kafka_groupId in logs changes to something like anonymous.e0f1240f-9b9b-494a-8b8a-63372c5123af, and deserialization works.
busrefreshendpoint –POST /busrefreshworks correctly in both named and anonymous group scenarios. The issue is specific tobusenv.
What I've Tried (with named group)
Adding explicit deserializer configuration for the consumer:
yaml
spring.cloud.stream.kafka.bindings.springCloudBusInput.consumer.configuration:
key.deserializer: org.apache.kafka.common.serialization.StringDeserializer
value.deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
Setting spring.json.trusted.packages and spring.json.value.default.type:
yaml
spring.cloud.stream.kafka.bindings.springCloudBusInput.consumer.properties:
spring.json.trusted.packages: "*"
spring.json.value.default.type: org.springframework.cloud.bus.event.RemoteApplicationEvent
None of these resolved the issue. The ClassCastException persists only when using a named group.id with /busenv.
Questions
Why does
EnvironmentBusRemoteApplicationEventdeserialization fail with a named consumer group, whileRefreshRemoteApplicationEventworks fine?Why does the anonymous group work perfectly for both event types?
What is the correct configuration to make
busenvwork with a named/persistentgroup.idin Kafka?
Additional Context
- I need a named
group.idbecause my Kafka cluster's ACL policy does not allow anonymous groups (they are blocked byGroupAuthorizationException). The anonymous group approach is not viable in production.
Any help would be greatly appreciated!