<dependency>
<groupId>com.fasterxml.jackson.datatypegroupId>
<artifactId>jackson-datatype-jsr310artifactId>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-typehandlers-jsr310artifactId>
<version>1.0.2version>
dependency>
替换 Webflux 自动装配的 ObjectMapper
/**
* 返回前端时, LocalDate, LocalDateTime, Date 返回时间戳处理
*/
@Configuration
public class JackSerializeConfiguration {
@Bean
@Order(0)
@Primary
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
// 时间项目推荐返回前端时间戳,前端根据需要自己转换格式
JavaTimeModule javaTimeModule = new JavaTimeModule();
// 自定义 全局把时间转为 时间戳
javaTimeModule.addSerializer(Date.class, new DateSerializer());
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer());
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
mapper.registerModule(javaTimeModule);
mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
return mapper;
}
}
/**
* jackson 处理 Date
*/
public class DateSerializer extends JsonSerializer<Date> {
@Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers)
throws IOException {
if (!Objects.isNull(value)) {
gen.writeNumber(value.getTime());
}
}
}
@Configuration
@EnableWebFlux
public class CustomWebfluxConfiguration implements WebFluxConfigurer{
@Autowired
private RequestedContentTypeResolver contentTypeResolver;
@Autowired
private ServerCodecConfigurer serverCodecConfigurer;
public CustomWebfluxConfiguration(ResourceProperties resourceProperties,
WebProperties webProperties,
WebFluxProperties webFluxProperties,
ListableBeanFactory beanFactory,
ObjectProvider<HandlerMethodArgumentResolver> resolvers,
ObjectProvider<CodecCustomizer> codecCustomizers,
ObjectProvider<ResourceHandlerRegistrationCustomizer>
resourceHandlerRegistrationCustomizer,
ObjectProvider<ViewResolver> viewResolvers) {
super(resourceProperties, webProperties, webFluxProperties, beanFactory,
resolvers, codecCustomizers, resourceHandlerRegistrationCustomizer, viewResolvers);
}
@Bean
public ResultDataResponseBodyHandler responseBodyResultHandler() {
return new ResultDataResponseBodyHandler(serverCodecConfigurer.getWriters(), contentTypeResolver);
}
}
如果以上一种方式配置自定义的 ResultDataResponseBodyHandler, 会覆盖webflux自动装配的 WebFluxConfig, 在 WebFluxConfig 中配置了一些默认的 HttpMessageConverter会自动失效,导致 自定义配置的 ObjectMapper处理时间戳的配置失效, 所以这种方式不行
下面是可行方案,可以使自定义的ResultDataResponseBodyHandler 和 ObjectMapper都生效, 直接继承WebFluxConfg, 这样就有了默认的 HttpMessageConverter, 我们自定义的ObjectMapper也就生效了
@Configuration
@EnableWebFlux
public class CustomWebfluxConfiguration
extends WebFluxAutoConfiguration.WebFluxConfig {
@Autowired
private RequestedContentTypeResolver contentTypeResolver;
@Autowired
private ServerCodecConfigurer serverCodecConfigurer;
public CustomWebfluxConfiguration(ResourceProperties resourceProperties,
WebProperties webProperties, WebFluxProperties webFluxProperties,
ListableBeanFactory beanFactory, ObjectProvider<HandlerMethodArgumentResolver> resolvers,
ObjectProvider<CodecCustomizer> codecCustomizers,
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizer,
ObjectProvider<ViewResolver> viewResolvers) {
super(resourceProperties, webProperties, webFluxProperties, beanFactory,
resolvers, codecCustomizers,
resourceHandlerRegistrationCustomizer, viewResolvers);
}
@Bean
public ResultDataResponseBodyHandler responseBodyResultHandler() {
return new ResultDataResponseBodyHandler(serverCodecConfigurer.getWriters(), contentTypeResolver);
}
}
{
"code": "200",
"msg": "success",
"subCode": null,
"subMsg": null,
"data": {
"id": 1,
"serviceId": "river-user",
"filters": null,
"predictes": null,
"orderSeq": 1,
"uri": "lb://river-user",
"enable": 1,
"createTime": 1618728527000,
"updateTime": 1618728533000,
"now": 1619020800000,
"start": 1619096944713
}
}