Hello Axoniq,
We are trying to implement a Custom JacksonSerialize with a custom payload type.
We have the same consideration as imnewone in Customize event payload type for adapting the JacksonSerializer.
If we make our own ShorterJacksonSerializer
we get into trouble with the builder of the JacksonSerializer in our AxonConfig.class
The ShorterJacksonSerializer.builder()
will return the JacksonSerializer
and not our own ShorterJacksonSerializer
with the overridden methods.
How can we resolve this without completely writing our own serializer?
We are using Axon framework v4.9.4 with Spring boor 3.2.x
@Configuration
public class AxonConfig {
@Bean
@Primary
public Serializer defaultSerializer() {
ObjectMapper mapper = ShorterJacksonSerializer.defaultSerializer().getObjectMapper();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.activateDefaultTyping(
mapper.getPolymorphicTypeValidator(),
DefaultTyping.OBJECT_AND_NON_CONCRETE
);
return ShorterJacksonSerializer.builder()
.objectMapper(mapper)
.lenientDeserialization()
.build();
}
}
public class ShorterJacksonSerializer extends JacksonSerializer {
String basePackage = "event";
protected ShorterJacksonSerializer(Builder builder) {
super(builder);
}
protected String resolveClassName(SerializedType serializedType) {
return basePackage + "." + serializedType.getName();
}
@Override
public SerializedType typeForClass(Class type) {
if (type.getName().startsWith(basePackage)) {
String shortName = "%s.%s".formatted(basePackage, type.getSimpleName());
return new SimpleSerializedType(shortName, getRevisionResolver().revisionOf(type));
} else {
return new SimpleSerializedType(type.getSimpleName(), this.getRevisionResolver().revisionOf(type));
}
}
}