我有来自Kafka主题的阅读avro消息的春季计划。 当消息读取发生异常时:
org.springframework.messaging.converter.MessageConversionException:无法从[com.mycompany.myapp.domain.avro.GreetingRecord]转换为[com.mycompany.myapp.domain.avro.GreetingRecord]
在调试中,我看到ClassUtils.isAssignable对同一个类返回false:
Class <?> targetClass = parameter.getParameterType();
Class <?> payloadClass = payload.getClass();
if (ClassUtils.isAssignable(targetClass, payloadClass)) { // return false for same class
this.validate(message, parameter, payload);
return payload;
} else {
if (this.converter instanceof SmartMessageConverter) {
SmartMessageConverter smartConverter = (SmartMessageConverter)this.converter;
payload = smartConverter.fromMessage(message, targetClass, parameter);
} else {
payload = this.converter.fromMessage(message, targetClass);
}
}
我发现targetClass和payloadClass中的类加载器不同( RestartClassLoader 和 Launcher $ AppClassLoader )
目标(有效载荷)类定义:
/**
* Autogenerated by Avro
*
* DO NOT EDIT DIRECTLY
*/
package com.mycompany.myapp.domain.avro;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.message.BinaryMessageEncoder;
import org.apache.avro.message.BinaryMessageDecoder;
import org.apache.avro.message.SchemaStore;
@SuppressWarnings("all")
@org.apache.avro.specific.AvroGenerated
public class GreetingRecord extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
private static final long serialVersionUID = -39777975679985249L;
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"GreetingRecord\",\"namespace\":\"com.mycompany.myapp.domain.avro\",\"fields\":[{\"name\":\"message\",\"type\":\"string\"}]}");
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
private static SpecificData MODEL$ = new SpecificData();
private static final BinaryMessageEncoder<GreetingRecord> ENCODER =
new BinaryMessageEncoder<GreetingRecord>(MODEL$, SCHEMA$);
private static final BinaryMessageDecoder<GreetingRecord> DECODER =
new BinaryMessageDecoder<GreetingRecord>(MODEL$, SCHEMA$);
/**
* Return the BinaryMessageDecoder instance used by this class.
*/
public static BinaryMessageDecoder<GreetingRecord> getDecoder() {
return DECODER;
}
/**
* Create a new BinaryMessageDecoder instance for this class that uses the specified {@link SchemaStore}.
* @param resolver a {@link SchemaStore} used to find schemas by fingerprint
*/
public static BinaryMessageDecoder<GreetingRecord> createDecoder(SchemaStore resolver) {
return new BinaryMessageDecoder<GreetingRecord>(MODEL$, SCHEMA$, resolver);
}
/** Serializes this GreetingRecord to a ByteBuffer. */
public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
return ENCODER.encode(this);
}
/** Deserializes a GreetingRecord from a ByteBuffer. */
public static GreetingRecord fromByteBuffer(
java.nio.ByteBuffer b) throws java.io.IOException {
return DECODER.decode(b);
}
@Deprecated public java.lang.CharSequence message;
/**
* Default constructor. Note that this does not initialize fields
* to their default values from the schema. If that is desired then
* one should use <code>newBuilder()</code>.
*/
public GreetingRecord() {}
/**
* All-args constructor.
* @param message The new value for message
*/
public GreetingRecord(java.lang.CharSequence message) {
this.message = message;
}
public org.apache.avro.Schema getSchema() { return SCHEMA$; }
// Used by DatumWriter. Applications should not call.
public java.lang.Object get(int field$) {
switch (field$) {
case 0: return message;
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
// Used by DatumReader. Applications should not call.
@SuppressWarnings(value="unchecked")
public void put(int field$, java.lang.Object value$) {
switch (field$) {
case 0: message = (java.lang.CharSequence)value$; break;
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
/**
* Gets the value of the 'message' field.
* @return The value of the 'message' field.
*/
public java.lang.CharSequence getMessage() {
return message;
}
/**
* Sets the value of the 'message' field.
* @param value the value to set.
*/
public void setMessage(java.lang.CharSequence value) {
this.message = value;
}
/**
* Creates a new GreetingRecord RecordBuilder.
* @return A new GreetingRecord RecordBuilder
*/
public static GreetingRecord.Builder newBuilder() {
return new GreetingRecord.Builder();
}
/**
* Creates a new GreetingRecord RecordBuilder by copying an existing Builder.
* @param other The existing builder to copy.
* @return A new GreetingRecord RecordBuilder
*/
public static GreetingRecord.Builder newBuilder(GreetingRecord.Builder other) {
return new GreetingRecord.Builder(other);
}
/**
* Creates a new GreetingRecord RecordBuilder by copying an existing GreetingRecord instance.
* @param other The existing instance to copy.
* @return A new GreetingRecord RecordBuilder
*/
public static GreetingRecord.Builder newBuilder(GreetingRecord other) {
return new GreetingRecord.Builder(other);
}
/**
* RecordBuilder for GreetingRecord instances.
*/
public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<GreetingRecord>
implements org.apache.avro.data.RecordBuilder<GreetingRecord> {
private java.lang.CharSequence message;
/** Creates a new Builder */
private Builder() {
super(SCHEMA$);
}
/**
* Creates a Builder by copying an existing Builder.
* @param other The existing Builder to copy.
*/
private Builder(GreetingRecord.Builder other) {
super(other);
if (isValidValue(fields()[0], other.message)) {
this.message = data().deepCopy(fields()[0].schema(), other.message);
fieldSetFlags()[0] = true;
}
}
/**
* Creates a Builder by copying an existing GreetingRecord instance
* @param other The existing instance to copy.
*/
private Builder(GreetingRecord other) {
super(SCHEMA$);
if (isValidValue(fields()[0], other.message)) {
this.message = data().deepCopy(fields()[0].schema(), other.message);
fieldSetFlags()[0] = true;
}
}
/**
* Gets the value of the 'message' field.
* @return The value.
*/
public java.lang.CharSequence getMessage() {
return message;
}
/**
* Sets the value of the 'message' field.
* @param value The value of 'message'.
* @return This builder.
*/
public GreetingRecord.Builder setMessage(java.lang.CharSequence value) {
validate(fields()[0], value);
this.message = value;
fieldSetFlags()[0] = true;
return this;
}
/**
* Checks whether the 'message' field has been set.
* @return True if the 'message' field has been set, false otherwise.
*/
public boolean hasMessage() {
return fieldSetFlags()[0];
}
/**
* Clears the value of the 'message' field.
* @return This builder.
*/
public GreetingRecord.Builder clearMessage() {
message = null;
fieldSetFlags()[0] = false;
return this;
}
@Override
@SuppressWarnings("unchecked")
public GreetingRecord build() {
try {
GreetingRecord record = new GreetingRecord();
record.message = fieldSetFlags()[0] ? this.message : (java.lang.CharSequence) defaultValue(fields()[0]);
return record;
} catch (java.lang.Exception e) {
throw new org.apache.avro.AvroRuntimeException(e);
}
}
}
@SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumWriter<GreetingRecord>
WRITER$ = (org.apache.avro.io.DatumWriter<GreetingRecord>)MODEL$.createDatumWriter(SCHEMA$);
@Override public void writeExternal(java.io.ObjectOutput out)
throws java.io.IOException {
WRITER$.write(this, SpecificData.getEncoder(out));
}
@SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumReader<GreetingRecord>
READER$ = (org.apache.avro.io.DatumReader<GreetingRecord>)MODEL$.createDatumReader(SCHEMA$);
@Override public void readExternal(java.io.ObjectInput in)
throws java.io.IOException {
READER$.read(this, SpecificData.getDecoder(in));
}
}
如何解决不同类加载器的问题?
答案 0 :(得分:0)
似乎是duplicate
这是因为该类已由两个不同的类加载器加载。你不能在他们之间施放。一定要只使用一个类加载器
答案 1 :(得分:0)
我在这里找到了解决方法:@AuthenticationPrincipal with Spring Boot not working
location.href = 'http://example.com';
}
关闭spring.devtools.restart解决问题:
答案 2 :(得分:0)
我有解决办法。只要您使用Spring,我猜您就可能使用Spring Devtools。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
删除导致麻烦的依赖项:
org.springframework.kafka.listener.ListenerExecutionFailedException: Listener method 'public void org.company.MyKafkaConsumer.consume(org.apache.kafka.clients.consumer.ConsumerRecord<java.lang.String, org.mycompany.avro.MyAvroObject>,org.springframework.kafka.support.Acknowledgment)' threw exception; nested exception is java.lang.ClassCastException: class org.mycompany.avro.MyAvroObject cannot be cast to class org.mycompany.avro.MyAvroObject (org.mycompany.avro.MyAvroObject is in unnamed module of loader 'app'; org.mycompany.avro.MyAvroObject is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @3a9df291); nested exception is java.lang.ClassCastException: class org.mycompany.avro.MyAvroObject cannot be cast to class org.mycompany.avro.MyAvroObject (org.mycompany.avro.MyAvroObject is in unnamed module of loader 'app'; org.mycompany.avro.MyAvroObject is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @3a9df291)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.decorateException(KafkaMessageListenerContainer.java:1899) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeErrorHandler(KafkaMessageListenerContainer.java:1887) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeRecordListener(KafkaMessageListenerContainer.java:1792) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeWithRecords(KafkaMessageListenerContainer.java:1719) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeRecordListener(KafkaMessageListenerContainer.java:1617) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeListener(KafkaMessageListenerContainer.java:1348) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.pollAndInvoke(KafkaMessageListenerContainer.java:1064) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.run(KafkaMessageListenerContainer.java:972) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Caused by: java.lang.ClassCastException: class org.mycompany.avro.MyAvroObject cannot be cast to class org.mycompany.avro.MyAvroObject (org.mycompany.avro.MyAvroObject is in unnamed module of loader 'app'; org.mycompany.avro.MyAvroObject is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @3a9df291)
at org.company.MyKafkaConsumer.consume(NiaKafkaConsumer.java:33) ~[classes/:na]
at jdk.internal.reflect.GeneratedMethodAccessor81.invoke(Unknown Source) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:171) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:120) ~[spring-messaging-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.kafka.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:48) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:334) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.onMessage(RecordMessagingMessageListenerAdapter.java:86) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.onMessage(RecordMessagingMessageListenerAdapter.java:51) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeOnMessage(KafkaMessageListenerContainer.java:1854) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeOnMessage(KafkaMessageListenerContainer.java:1836) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeRecordListener(KafkaMessageListenerContainer.java:1779) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
... 8 common frames omitted