Open rujche opened 2 weeks ago
There is respective option to set such a converter:
/**
* Set the bean name of a
* {@link org.springframework.messaging.converter.SmartMessageConverter} (such as the
* {@link org.springframework.messaging.converter.CompositeMessageConverter}) to use
* in conjunction with the
* {@link org.springframework.messaging.MessageHeaders#CONTENT_TYPE} header to perform
* the conversion to the required type. If a SpEL expression is provided
* ({@code #{...}}), the expression can either evaluate to a
* {@link org.springframework.messaging.converter.SmartMessageConverter} instance or a
* bean name.
* @return the bean name.
* @since 2.7.1
*/
String contentTypeConverter() default "";
However I'm not sure what converter implementation you can use for Avro. We don't have one out-of-the-box.
Steps:
avro-maven-plugin
inpom.xml
.1.2. Add avro file (SampleMessage.avsc) in src/main/resources folder.
1.3. Run
mvn clean install
to generate code (SampleMessage.java).import org.apache.avro.generic.GenericArray; import org.apache.avro.specific.SpecificData; import org.apache.avro.util.Utf8; import org.apache.avro.message.BinaryMessageEncoder; import org.apache.avro.message.BinaryMessageDecoder; import org.apache.avro.message.SchemaStore;
@org.apache.avro.specific.AvroGenerated public class SampleMessage extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord { private static final long serialVersionUID = -4620837119995760321L;
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"SampleMessage\",\"namespace\":\"rujche.sample.avsc.generated\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"description\",\"type\":\"string\"}]}"); public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
private static final SpecificData MODEL$ = new SpecificData();
private static final BinaryMessageEncoder ENCODER =
new BinaryMessageEncoder(MODEL$, SCHEMA$);
private static final BinaryMessageDecoder DECODER =
new BinaryMessageDecoder(MODEL$, SCHEMA$);
... }
Expected behavior: message converted to
SampleMessage
successfully. Current Behavior: throw exception like this:Workaround: Use
GenericMessage<Object>
instead ofSampleMessage
:Related codes: https://github.com/spring-projects/spring-kafka/blob/98957e0c6c97a42830624eef6de3a6f2eecb521a/spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessor.java#L1204-L1215