此处为webflux 返回json时格式化时间字段
@Configuration
public class WebConfig implements WebFluxConfigurer {
@Bean
@Primary
public ObjectMapper mapper() {
ObjectMapper mapper = new ObjectMapper();
JavaTimeModule timeModule = new JavaTimeModule();
timeModule.addSerializer(Date.class, new DateSerializer(Boolean.FALSE, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));
timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
timeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
timeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
timeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
timeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
mapper.registerModule(timeModule).registerModule(new ParameterNamesModule()).registerModules(ObjectMapper.findModules());
return mapper;
}
@Override
public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
ObjectMapper mapper = mapper();
configurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(mapper));
configurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(mapper));
}
}
此处为reactiveRedis 序列化和反序列化时配置
@Bean(name = "reactiveRedisTemplate")
public ReactiveRedisTemplate<String, Object> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) throws JsonProcessingException {
RedisSerializationContext.RedisSerializationContextBuilder<String, Object> builder = RedisSerializationContext.<String, Object>newSerializationContext();
ObjectMapper mapper = new ObjectMapper();
JavaTimeModule timeModule = new JavaTimeModule();
timeModule.addSerializer(Date.class, new DateSerializer(Boolean.FALSE, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));
timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
timeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
timeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
timeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
timeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
mapper.registerModule(timeModule);
mapper.registerModules(ObjectMapper.findModules()).registerModule(new ParameterNamesModule());
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
serializer.setObjectMapper(mapper);
builder.key(new StringRedisSerializer());
builder.value(serializer);
builder.hashKey(new StringRedisSerializer());
builder.hashValue(serializer);
return new ReactiveRedisTemplate<String, Object>(factory, builder.build());
}
mapper.registerModule 注册时注意顺序 自定义必须先注册因为注册时内部进行了判断如果存在则不进行注册。
// then module itself
if (isEnabled(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS)) {
Object typeId = module.getTypeId();
if (typeId != null) {
if (_registeredModuleTypes == null) {
// plus let's keep them in order too, easier to debug or expose
// in registration order if that matter
_registeredModuleTypes = new LinkedHashSet<Object>();
}
// try adding; if already had it, should skip
if (!_registeredModuleTypes.add(typeId)) {
return this;
}
}
}