英文:
Java Jackson deserialize abstract class child which implements interface
问题
我有一个接口:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = ChildA.class, name = "childA"),
@JsonSubTypes.Type(value = ChildB.class, name = "childB"),
})
public interface Event {
}
然后是实现该接口的抽象类:
public abstract class SpecificEvent implements Event {
private ZonedDateTime timestamp;
}
最后是抽象类的子类:
public class ChildA extends SpecificEvent {
}
public class ChildB extends SpecificEvent {
}
Jackson 在反序列化子类时出现错误:
无法将类型标识 'childA' 解析为
packages.SpecificEvent
的子类型。
我做错了什么?
更新:
我正在通过 RabbitMQ 消费子类的消息。
Rabbit 配置:
public ObjectMapper objectMapper() {
var mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.registerModule(new Jdk8Module());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
mapper.setDateFormat(StdDateFormat.instance
.withTimeZone(TimeZone.getTimeZone(ZoneId.of("Europe/Moscow")))
.withLocale(new Locale("ru"))
);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper;
}
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter(objectMapper());
}
发送事件只需使用 RabbitTemplate:
rabbitTemplate.convertAndSend(
exchange.getName(),
event.getEventRouteKey().getKey(),
event
);
通过 @RabbitListener 监听消息。
英文:
I have an interface:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = ChildA.class, name = "childA"),
@JsonSubTypes.Type(value = ChildB.class, name = "childB"),
})
public interface Event {
}
And then abstract class which implements this interface:
public abstract class SpecificEvent implements Event {
private ZonedDateTime timestamp;
}
And finally children of abstract class:
public class ChildA extends SpecificEvent {
}
public class ChildB extends SpecificEvent {
}
Jackson fails to deserialize children with error:
> Could not resolve type id 'childA' as a subtype of
> packages.SpecificEvent
.
What am i doing wrong?
UPD
I'm consuming messages of children throw RabbitMQ.
Rabbit config:
public ObjectMapper objectMapper() {
var mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.registerModule(new Jdk8Module());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
mapper.setDateFormat(StdDateFormat.instance
.withTimeZone(TimeZone.getTimeZone(ZoneId.of("Europe/Moscow")))
.withLocale(new Locale("ru"))
);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper;
}
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter(objectMapper());
}
For sending events i just use RabbitTemplate:
rabbitTemplate.convertAndSend(
exchange.getName(),
event.getEventRouteKey().getKey(),
event
);
And listen to it via @RabbitListener.
答案1
得分: 0
实际上,在更改模型之前,我忘记在Rabbit中清除队列,这就是为什么我收到了反序列化错误。
英文:
Actually, i forgot to purge queue in Rabbit, before changing the models, that's why i got deserialization error.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论