Springboot中Instant时间传参及序列化详解

 更新时间:2023年11月06日 10:27:37   作者:杨某人信了你的邪  
这篇文章主要介绍了Springboot中Instant时间传参及序列化详解,Instant是Java8引入的一个精度极高的时间类型,可以精确到纳秒,但实际使用的时候不需要这么高的精确度,通常到毫秒就可以了,需要的朋友可以参考下

Instant时间传参及序列化

在部分场景中,后台的时间属性用的不是Date或Long,而是Instant,Java8引入的一个精度极高的时间类型,可以精确到纳秒,但实际使用的时候不需要这么高的精确度,通常到毫秒就可以了。

而在前后端传参的时候需要对Instant类型进行序列化及反序列化等处理,默认情况下,ObjectMapper是不支持序列化Instant类型的,需要注册JavaTimeModule才行,而且序列化的结果也不是时间戳,测试如下

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.time.Instant;

/**
 * Instant Jackson测试
 *
 * @author yangguirong
 */
@Slf4j
public class InstantTest {

    ObjectMapper objectMapper = new ObjectMapper();

    @Test
    void serializeTest() throws JsonProcessingException {
        objectMapper.registerModule(new JavaTimeModule());
        String str = objectMapper.writeValueAsString(Instant.now());
        log.info("serializeTest: {}", str);
        // serializeTest: 1691208180.052185000
    }

    @Test
    void deserializeTest() throws JsonProcessingException {
        objectMapper.registerModule(new JavaTimeModule());
        Instant instant = objectMapper.readValue("1691208180.052185000", Instant.class);
        log.info("deserializeTest: {}", instant);
        // deserializeTest: 2023-08-05T04:03:00.052185Z
    }
}

想要将其序列化为毫秒时间戳,需要对序列化及反序列化进行自定义

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.time.Instant;

/**
 * 自定义Instant序列化及反序列
 *
 * @author yangguirong
 */
public class InstantMillsTimeModule extends SimpleModule {

    public InstantMillsTimeModule() {
        this.addSerializer(Instant.class, new InstantMillisecondsSerializer());
        this.addDeserializer(Instant.class, new InstantMillisecondsDeserializer());
    }

    public static class InstantMillisecondsSerializer extends JsonSerializer<Instant> {
        @Override
        public void serialize(Instant instant, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            if (instant == null) {
                jsonGenerator.writeNull();
            } else {
                jsonGenerator.writeNumber(instant.toEpochMilli());
            }
        }
    }
    
    @Slf4j
    public static class InstantMillisecondsDeserializer extends JsonDeserializer<Instant> {
        @Override
        public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            try {
                long mills = p.getValueAsLong();
                return mills > 0 ? Instant.ofEpochMilli(mills) : null;
            } catch (Exception e) {
                log.error("Instant类型反序列化失败!val: {}, message: {}", p.getText(), e.getMessage());
            }
            return null;
        }
    }
}

再来测试一下自定义的序列化及反序列化方式

import com.example.websocket.config.InstantMillsTimeModule;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.time.Instant;

/**
 * Instant Jackson测试
 *
 * @author yangguirong
 */
@Slf4j
public class InstantTest {

    ObjectMapper objectMapper = new ObjectMapper();

    @Test
    void serializeTest() throws JsonProcessingException {
        objectMapper.registerModule(new JavaTimeModule());
        String str = objectMapper.writeValueAsString(Instant.now());
        log.info("serialize: {}", str);
        // serialize: 1691208180.052185000
    }

    @Test
    void deserializeTest() throws JsonProcessingException {
        objectMapper.registerModule(new JavaTimeModule());
        Instant instant = objectMapper.readValue("1691208180.052185000", Instant.class);
        log.info("deserialize: {}", instant);
        // deserialize: 2023-08-05T04:03:00.052185Z
    }

    @Test
    void millsSerializeTest() throws JsonProcessingException {
        objectMapper.registerModule(new InstantMillsTimeModule());
        String str = objectMapper.writeValueAsString(Instant.now());
        log.info("millsSerializeTest: {}", str);
        // millsSerializeTest: 1691208541018
    }

    @Test
    void millsDeserializeTest() throws JsonProcessingException {
        objectMapper.registerModule(new InstantMillsTimeModule());
        Instant instant = objectMapper.readValue("1691208541018", Instant.class);
        log.info("millsDeserializeTest: {}", instant);
        // deserialize: 2023-08-05T04:09:01.018Z
    }
}

可以看到,结果是符合预期的,可以在毫秒时间戳和Instant之间相互转换。

在后台配置SpringBoot的时候,需要考虑两种情况,一种就是Instant作为RequestParam/PathVariable的情况,另一种是RequestBody/ResponseBody的情况。前者借助转换器实现,配置如下

import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.Instant;

/**
 * web mvc设置
 *
 * @author yangguirong
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(instantConvert());
    }

    public Converter<String, Instant> instantConvert() {
        // 不能替换为lambda表达式
        return new Converter<String, Instant>() {
            @Override
            public Instant convert(String source) {
                if (StringUtils.hasText(source)) {
                    return Instant.ofEpochMilli(Long.parseLong(source));
                }
                return null;
            }
        };
    }
}

后者如果是局部配置,则在具体的实体类属性上添加@JsonSerialize和@JsonDeserialize注解,在注解中指定序列化器和反序列化器即可。如果是全局配置,则可以按照如下方式进行配置,将InstantMillsTimeModule注册为Bean,这个Bean会在JacksonAutoConfiguration中的StandardJackson2ObjectMapperBuilderCustomizer被自动注入,然后进行注册。

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Jackson配置
 *
 * @author yangguirong
 */
@Configuration
@AutoConfigureBefore(JacksonAutoConfiguration.class)
public class JacksonCustomizerConfig {

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jacksonModuleRegistryCustomizer() {
        return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.featuresToDisable(
                DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, SerializationFeature.FAIL_ON_EMPTY_BEANS
        );
    }

    @Bean
    public InstantMillsTimeModule instantMillsTimeModule() {
        return new InstantMillsTimeModule();
    }
}

简单的接口测试

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;

/**
 * instant测试
 *
 * @author yangguirong
 */
@Slf4j
@RequestMapping("instant")
@RestController
public class InstantTestController {

    @GetMapping("getInstant")
    public Instant getInstant() {
        return Instant.now();
    }

    @PutMapping("setInstant")
    public void setInstant(@RequestParam Instant instant) {
        log.info("setInstant: {}", instant);
    }

    @GetMapping("getInstantDemoVO")
    public DemoVO getInstantDemoVO() {
        return new DemoVO(Instant.now());
    }

    @PutMapping("setInstantDemoVO")
    public void setInstantDemoVO(@RequestBody DemoVO vo) {
        log.info("setInstantDemoVO:{}", vo);
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    static class DemoVO {
        private Instant instant;
    }
}

到此这篇关于Springboot中Instant时间传参及序列化详解的文章就介绍到这了,更多相关Instant时间传参及序列化内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • mybatis plus CU自动填充 和 软删除自动填充的实现方法

    mybatis plus CU自动填充 和 软删除自动填充的实现方法

    这篇文章主要介绍了mybatis plus CU自动填充 和 软删除自动填充的实现方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-07-07
  • Spring Boot 与 Kotlin 使用JdbcTemplate连接MySQL数据库的方法

    Spring Boot 与 Kotlin 使用JdbcTemplate连接MySQL数据库的方法

    本文介绍在Spring Boot基础下配置数据源和通过 JdbcTemplate 编写数据访问的示例。感兴趣的朋友跟随脚本之家小编一起学习吧
    2018-01-01
  • 6个必备的Java并发面试种子题目合集

    6个必备的Java并发面试种子题目合集

    并发是Java面试的经常会考到的知识点,这篇文章主要为大家整理了6个必备的Java并发面试种子题目,文中的示例代码简洁易懂,需要的可以学习一下
    2023-07-07
  • idea切换git地址并刷新右下角git分支

    idea切换git地址并刷新右下角git分支

    这篇文章主要介绍了idea切换git地址并刷新右下角git分支,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Mybatis中的mapper是如何和XMl关联起来的

    Mybatis中的mapper是如何和XMl关联起来的

    这篇文章主要介绍了Mybatis中的mapper是如何和XMl关联起来的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • IntelliJ IDEA全局内容搜索和替换教程图解

    IntelliJ IDEA全局内容搜索和替换教程图解

    很多朋友在做项目时,会在整个项目里活指定文件夹下进行全局搜索和替换,下面小编给大家带来了IntelliJ IDEA全局内容搜索和替换教程图解,需要的朋友参考下吧
    2018-04-04
  • java集合与数组的相同点和不同点

    java集合与数组的相同点和不同点

    今天小编就为大家分享一篇关于java集合与数组的相同点和不同点,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-04-04
  • Myeclipse 2016下Aptana安装教程

    Myeclipse 2016下Aptana安装教程

    这篇文章主要为大家详细介绍了Myeclipse 2016下Aptana安装教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • 一文带你掌握Java ImageIO类

    一文带你掌握Java ImageIO类

    Java中的ImageIO类是Java标准库中用于处理图像的一个非常常用的 API,它提供了读取和写入多种常见图像格式的功能,如JPEG、PNG、BMP、GIF等,本文将全面详细地介绍Java中的ImageIO类的使用方法,需要的朋友可以参考下
    2023-05-05
  • Java实现多路复用select模型实例详解

    Java实现多路复用select模型实例详解

    在计算机网络中,多路复用(Multiplexing)指的是通过一种机制将多个 I/O 操作合并到同一个线程或进程中,从而提高系统的效率,在 Java 中,可以使用 Selector 类来实现基于 I/O 多路复用的模式,故本文给大家介绍了Java实现多路复用select模型实例,需要的朋友可以参考下
    2025-03-03

最新评论