写点什么

HttpMessageConverter 添加 java8 LocateTime 时间转换

作者:智慧源点
  • 2024-03-05
    北京
  • 本文字数:2150 字

    阅读完需:约 7 分钟

HttpMessageConverter 简介

HttpMessageConverter 是 SpringMVC 中提供的一个策略接口,它是一个消息转换器类,Spring Mvc 中就是由 HttpMessageConverter 负责转换 HTTP 的请求和响应。

1、jackson 方式 converter 实现

@Configuration@ComponentScan(basePackages = {"com.ele.common"})public class WebMvcConfig extends RestWebConfig implements WebMvcConfigurer {
@Autowired private RequestHeaderInterceptor requestHeaderInterceptor;
@Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(requestHeaderInterceptor); }
@Override public void addFormatters(FormatterRegistry registry) {// registry.addConverter(new LongDateConverter()); }
@Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { HttpMessageConverter<?> converter = converters.stream().filter(e -> e instanceof MappingJackson2HttpMessageConverter).findFirst().orElse(null); if (converter != null) { MappingJackson2HttpMessageConverter jackson = (MappingJackson2HttpMessageConverter) converter; ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8")); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE); JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DatePattern.NORM_DATETIME_FORMATTER)); javaTimeModule.addSerializer(LocalDate.class,new LocalDateSerializer(DatePattern.NORM_DATE_FORMATTER)); javaTimeModule.addSerializer(LocalTime.class,new LocalTimeSerializer(DatePattern.NORM_TIME_FORMATTER)); javaTimeModule.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(DatePattern.NORM_DATETIME_FORMATTER)); javaTimeModule.addDeserializer(LocalDate.class,new LocalDateDeserializer(DatePattern.NORM_DATE_FORMATTER)); javaTimeModule.addDeserializer(LocalTime.class,new LocalTimeDeserializer(DatePattern.NORM_TIME_FORMATTER)); objectMapper.registerModule(javaTimeModule).registerModule(new ParameterNamesModule()); jackson.setObjectMapper(objectMapper); converters.add(0, jackson); } } @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") // 所有接口 .allowCredentials(true) // 是否发送 Cookie .allowedOriginPatterns("*") // 支持域 .allowedMethods("GET", "POST", "PUT", "DELETE") // 支持方法 .allowedHeaders("*") .exposedHeaders("*"); }}
复制代码
2、fastjson 方式 converter 实现
 /**     * 使用fastJson 转换     * 字段强转string使用注解方式  @JSONField(serializeUsing = ToStringSerializer.class)     *     * @return fastJson http消息转换器     */    @Bean    public HttpMessageConverters fastJsonMessageConverters() {        List<HttpMessageConverter<?>> converters = new ArrayList<>();        //需要定义一个convert转换消息的对象;        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();        //添加fastJson的配置信息;        FastJsonConfig fastJsonConfig = new FastJsonConfig();        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue);        //fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);        //全局时间配置//        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");//        fastJsonConfig.setCharset(Charset.forName("UTF-8"));        //处理中文乱码问题        List<MediaType> fastMediaTypes = new ArrayList<>();        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);        //在convert中添加配置信息.        fastConverter.setSupportedMediaTypes(fastMediaTypes);        fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(0, fastConverter); return new HttpMessageConverters(converters); }
复制代码

有你所需的学习资料:极客时间、图灵、Python、AI 等,微信联系:wytwhdwdd

用户头像

智慧源点

关注

终身学习、研究java架构、ai大模型 2019-12-06 加入

商业合作: wytwhdwdd

评论

发布
暂无评论
HttpMessageConverter添加java8 LocateTime时间转换_智慧源点_InfoQ写作社区