环境
rocketmq-spring-boot-2.2.1
问题描述
项目中日期类一般使用Date类型,如果使用LocalDateTime,它是Java1.8新的日期类型,对这个类型序列化不做特殊处理会报错
新增RocketConfig配置类,配置rocketMQTemplate,给他增加一个MyMQMessageConverter
@Configuration
public class RocketConfig implements ApplicationContextAware {
public static final String PRODUCER_BEAN_NAME = "defaultMQProducer";
public static final String CONSUMER_BEAN_NAME = "defaultLitePullConsumer";
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Bean(destroyMethod = "destroy")
public RocketMQTemplate rocketMQTemplate(MyMQMessageConverter myMQMessageConverter) {
RocketMQTemplate rocketMQTemplate = new RocketMQTemplate();
if (applicationContext.containsBean(PRODUCER_BEAN_NAME)) {
rocketMQTemplate.setProducer((DefaultMQProducer) applicationContext.getBean(PRODUCER_BEAN_NAME));
}
if (applicationContext.containsBean(CONSUMER_BEAN_NAME)) {
rocketMQTemplate.setConsumer((DefaultLitePullConsumer) applicationContext.getBean(CONSUMER_BEAN_NAME));
}
//设置消息转换器,处理LocalDateTime类型
rocketMQTemplate.setMessageConverter(myMQMessageConverter.getMessageConverter());
return rocketMQTemplate;
}
@Bean
public MyMQMessageConverter createRocketMQMessageConverter(){
return new MyMQMessageConverter();
}
}
复制了原来的RocketMQMessageConverter,增加JavaTimeModule 处理
public class MyMQMessageConverter extends RocketMQMessageConverter{
private static final boolean JACKSON_PRESENT;
private static final boolean FASTJSON_PRESENT;
static {
ClassLoader classLoader = RocketMQMessageConverter.class.getClassLoader();
JACKSON_PRESENT =
ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader) &&
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", classLoader);
FASTJSON_PRESENT = ClassUtils.isPresent("com.alibaba.fastjson.JSON", classLoader) &&
ClassUtils.isPresent("com.alibaba.fastjson.support.config.FastJsonConfig", classLoader);
}
private final CompositeMessageConverter messageConverter;
public MyMQMessageConverter() {
List<MessageConverter> messageConverters = new ArrayList<>();
ByteArrayMessageConverter byteArrayMessageConverter = new ByteArrayMessageConverter();
byteArrayMessageConverter.setContentTypeResolver(null);
messageConverters.add(byteArrayMessageConverter);
messageConverters.add(new StringMessageConverter());
if (JACKSON_PRESENT) {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
ObjectMapper mapper = converter.getObjectMapper();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
//LocalDatetime序列化
JavaTimeModule timeModule = new JavaTimeModule();
timeModule.addDeserializer(LocalDate.class,
new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
timeModule.addDeserializer(LocalDateTime.class,
new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
timeModule.addSerializer(LocalDate.class,
new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
timeModule.addSerializer(LocalDateTime.class,
new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
mapper.registerModule(timeModule);
converter.setObjectMapper(mapper);
messageConverters.add(converter);
}
if (FASTJSON_PRESENT) {
try {
messageConverters.add(
(MessageConverter)ClassUtils.forName(
"com.alibaba.fastjson.support.spring.messaging.MappingFastJsonMessageConverter",
ClassUtils.getDefaultClassLoader()).newInstance());
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException ignored) {
//ignore this exception
}
}
messageConverter = new CompositeMessageConverter(messageConverters);
}
public MessageConverter getMessageConverter() {
return messageConverter;
}
}
其实官方也修复了这个bug,修复版本还没有release。官方修复序列化