How to serialize message from kafka topic

i use axon with mongodb and kafka.
i recieved issue.

how to solve this?

com.thoughtworks.xstream.converters.ConversionException: 
---- Debugging information ----
cause-exception     : com.thoughtworks.xstream.security.ForbiddenClassException
cause-message       : org.apache.kafka.common.TopicPartition
class               : java.util.HashMap
required-type       : java.util.HashMap
converter-type      : com.thoughtworks.xstream.converters.collections.MapConverter
path                : /org.axonframework.extensions.kafka.eventhandling.consumer.streamable.KafkaTrackingToken/positions/m/entry/org.apache.kafka.common.TopicPartition
line number         : 1
class[1]            : java.util.Collections$UnmodifiableMap
required-type[1]    : java.util.Collections$UnmodifiableMap
converter-type[1]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
class[2]            : org.axonframework.extensions.kafka.eventhandling.consumer.streamable.KafkaTrackingToken
required-type[2]    : org.axonframework.extensions.kafka.eventhandling.consumer.streamable.KafkaTrackingToken
version             : 1.4.18
-------------------------------

and if i use this

axon:
  serializer:
    general: jackson
    events: jackson
    messages: jackson

change error to org.axonframework.serialization.SerializationException: Error while deserializing object

Hi imnewone,

Could it be that the message is serialized in XML and the application expects Jackson? What happens if you remove the config for the general serializer (which means it will use Xstream).

I hope this helps,

KR,

Yvonne

It could also be an XStream security issue see java - com.thoughtworks.xstream.security.ForbiddenClassException - Stack Overflow

I’m getting a similar exception from XStream, but my code is not using it to deserialize XML, the axonframework is to deserialize the tracking token in a TrackingEventProcessor.

See here:


com.thoughtworks.xstream.security.ForbiddenClassException: org.apache.kafka.common.TopicPartition
	at com.thoughtworks.xstream.security.NoTypePermission.allows(NoTypePermission.java:26) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.mapper.SecurityMapper.realClass(SecurityMapper.java:74) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:47) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readBareItem(AbstractCollectionConverter.java:131) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:117) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readCompleteItem(AbstractCollectionConverter.java:147) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.converters.collections.MapConverter.putCurrentEntryIntoMap(MapConverter.java:106) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.converters.collections.MapConverter.populateMap(MapConverter.java:99) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.converters.collections.MapConverter.populateMap(MapConverter.java:93) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.converters.collections.MapConverter.unmarshal(MapConverter.java:88) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:74) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:72) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:68) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:499) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:425) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:277) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:74) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:72) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:68) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:499) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:425) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:277) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:74) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:72) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:68) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:52) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:136) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1421) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1399) ~[xstream-1.4.19.jar:1.4.19]
	at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1284) ~[xstream-1.4.19.jar:1.4.19]
	at org.axonframework.serialization.xml.XStreamSerializer.doDeserialize(XStreamSerializer.java:141) ~[axon-messaging-4.5.14.jar:4.5.14]
	at org.axonframework.serialization.AbstractXStreamSerializer.deserialize(AbstractXStreamSerializer.java:160) ~[axon-messaging-4.5.14.jar:4.5.14]
	at org.axonframework.eventhandling.tokenstore.AbstractTokenEntry.getToken(AbstractTokenEntry.java:121) ~[axon-messaging-4.5.14.jar:4.5.14]
	at org.axonframework.eventhandling.tokenstore.jpa.JpaTokenStore.fetchToken(JpaTokenStore.java:197) ~[axon-messaging-4.5.14.jar:4.5.14]
	at org.axonframework.eventhandling.TrackingEventProcessor$WorkerLauncher.lambda$run$2(TrackingEventProcessor.java:1134) ~[axon-messaging-4.5.14.jar:4.5.14]
	at org.axonframework.common.transaction.TransactionManager.executeInTransaction(TransactionManager.java:47) ~[axon-messaging-4.5.14.jar:4.5.14]
	at org.axonframework.eventhandling.TrackingEventProcessor$WorkerLauncher.run(TrackingEventProcessor.java:1133) ~[axon-messaging-4.5.14.jar:4.5.14]
	at java.base/java.lang.Thread.run(Thread.java:829) ~[na:na]

This is a blocking problem for us.

I did notice a comment of Allard’s in the code that instantiates the XStream parser saying it might not work in non-sun/oracle JVMs. I’m using Amazon Corretto 11… perhaps that’s a problem?

Oh… and I’m using the Axon Kafka Extension - hence the KafkaTopic in the serialized token

Using XStream, especially in combination with classes like HashMap, are problematic. Moving to JSON would probably be the easiest solution. If possible, you could remove the tokens, and reinitialize them in JSON. It’s possible but not trivial to transform the stored XML’s to JSON if you want to keep the location in the event stream.

Thanks - we did move to json to fix the problem, but the real problem turned out to be what I think is a bug in the KafkaExtension: you see it in my stacktrace above…
org.apache.kafka.common.TopicPartition is not whitelisted by XStream unlike most of the other axon classes. Switching to Jackson overcomes this problem, but it’s a bug I think.