我有一个java模块,它包含实体(根据Evan's definition)。
此模块应在spring-boot
项目中使用,我需要通过消息传递表示的信息。
我更希望将序列化(也称为jackson
- 特定)逻辑与模块分开,因此我在spring项目中添加了jackson-module-parameter-names
和maven-compiler-plugin
,试图关注this article
配置如下:
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new ParameterNamesModule());
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper.registerModule(new JavaTimeModule());
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
return mapper;
}
@Bean
public MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
converter.setObjectMapper(objectMapper());
return converter;
}
问题:如果没有直接注释/添加任何类型的配置,jackson是否可以序列化/反序列化对象?
由于代码中的抽象,挑战是模型的结构。该模型如下:
public abstract class AbstractParent {
//contains constructor to make sure values are passed during creation time
}
public abstract class AbstractKid1 extends AbstractParent {
//contains constructor
}
public abstract class AbstractKid2 extends AbstractParent {
//contains constructor
}
public class ConcreteKid1 extends AbstractKid1 {
}
等等..
我希望能够发布/接收AbstractParent
个对象,然后能够处理具体类型。
如何在不更改模块的情况下“告知”jackson关于抽象类的子类型(jackson-polymorphism)?
Caused by: org.springframework.jms.support.converter.MessageConversionException: Failed to convert JSON message content; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.example.ConcreteKid1: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
; line: 2, column: 3]
at org.springframework.jms.support.converter.MappingJackson2MessageConverter.fromMessage(MappingJackson2MessageConverter.java:232) ~[spring-jms-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener.extractMessage(AbstractAdaptableMessageListener.java:218) ~[spring-jms-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener$MessagingMessageConverterAdapter.extractPayload(AbstractAdaptableMessageListener.java:438) ~[spring-jms-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener$MessagingMessageConverterAdapter$LazyResolutionMessage.unwrapPayload(AbstractAdaptableMessageListener.java:501) ~[spring-jms-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener$MessagingMessageConverterAdapter$LazyResolutionMessage.getPayload(AbstractAdaptableMessageListener.java:483) ~[spring-jms-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver.resolveArgument(PayloadArgumentResolver.java:113) ~[spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:112) ~[spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:135) ~[spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:107) ~[spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:104) ~[spring-jms-4.3.7.RELEASE.jar:4.3.7.RELEASE]
... 10 more
提前谢谢!