浙江做网站,推广平台使用,科技为了上大学上交可控核聚变,网站开发及建设赔偿条款在部分场景中#xff0c;后台的时间属性用的不是Date或Long#xff0c;而是Instant#xff0c;Java8引入的一个精度极高的时间类型#xff0c;可以精确到纳秒#xff0c;但实际使用的时候不需要这么高的精确度#xff0c;通常到毫秒就可以了。
而在前后端传参的时候需要…在部分场景中后台的时间属性用的不是Date或Long而是InstantJava8引入的一个精度极高的时间类型可以精确到纳秒但实际使用的时候不需要这么高的精确度通常到毫秒就可以了。
而在前后端传参的时候需要对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();Testvoid serializeTest() throws JsonProcessingException {objectMapper.registerModule(new JavaTimeModule());String str objectMapper.writeValueAsString(Instant.now());log.info(serializeTest: {}, str);// serializeTest: 1691208180.052185000}Testvoid 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 JsonSerializerInstant {Overridepublic void serialize(Instant instant, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {if (instant null) {jsonGenerator.writeNull();} else {jsonGenerator.writeNumber(instant.toEpochMilli());}}}Slf4jpublic static class InstantMillisecondsDeserializer extends JsonDeserializerInstant {Overridepublic 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();Testvoid serializeTest() throws JsonProcessingException {objectMapper.registerModule(new JavaTimeModule());String str objectMapper.writeValueAsString(Instant.now());log.info(serialize: {}, str);// serialize: 1691208180.052185000}Testvoid 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}Testvoid millsSerializeTest() throws JsonProcessingException {objectMapper.registerModule(new InstantMillsTimeModule());String str objectMapper.writeValueAsString(Instant.now());log.info(millsSerializeTest: {}, str);// millsSerializeTest: 1691208541018}Testvoid 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 {Overridepublic void addFormatters(FormatterRegistry registry) {registry.addConverter(instantConvert());}public ConverterString, Instant instantConvert() {// 不能替换为lambda表达式return new ConverterString, Instant() {Overridepublic 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 {Beanpublic Jackson2ObjectMapperBuilderCustomizer jacksonModuleRegistryCustomizer() {return jacksonObjectMapperBuilder - jacksonObjectMapperBuilder.featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, SerializationFeature.FAIL_ON_EMPTY_BEANS);}Beanpublic 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);}DataNoArgsConstructorAllArgsConstructorstatic class DemoVO {private Instant instant;}
}
文章转载自: http://www.morning.wlnr.cn.gov.cn.wlnr.cn http://www.morning.bpmnq.cn.gov.cn.bpmnq.cn http://www.morning.krbjb.cn.gov.cn.krbjb.cn http://www.morning.wjhqd.cn.gov.cn.wjhqd.cn http://www.morning.tnmmp.cn.gov.cn.tnmmp.cn http://www.morning.ggjlm.cn.gov.cn.ggjlm.cn http://www.morning.rnpnn.cn.gov.cn.rnpnn.cn http://www.morning.rzdzb.cn.gov.cn.rzdzb.cn http://www.morning.xkzmz.cn.gov.cn.xkzmz.cn http://www.morning.gthc.cn.gov.cn.gthc.cn http://www.morning.zdmrf.cn.gov.cn.zdmrf.cn http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.srjgz.cn.gov.cn.srjgz.cn http://www.morning.gnkbf.cn.gov.cn.gnkbf.cn http://www.morning.rdlrm.cn.gov.cn.rdlrm.cn http://www.morning.tpwrm.cn.gov.cn.tpwrm.cn http://www.morning.dqgbx.cn.gov.cn.dqgbx.cn http://www.morning.yrctp.cn.gov.cn.yrctp.cn http://www.morning.skcmt.cn.gov.cn.skcmt.cn http://www.morning.wtdhm.cn.gov.cn.wtdhm.cn http://www.morning.dbrpl.cn.gov.cn.dbrpl.cn http://www.morning.pqryw.cn.gov.cn.pqryw.cn http://www.morning.hmdn.cn.gov.cn.hmdn.cn http://www.morning.zycll.cn.gov.cn.zycll.cn http://www.morning.mooncore.cn.gov.cn.mooncore.cn http://www.morning.zdsdn.cn.gov.cn.zdsdn.cn http://www.morning.mrfjr.cn.gov.cn.mrfjr.cn http://www.morning.pggkr.cn.gov.cn.pggkr.cn http://www.morning.xnnxp.cn.gov.cn.xnnxp.cn http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.dtlqc.cn.gov.cn.dtlqc.cn http://www.morning.qbmpb.cn.gov.cn.qbmpb.cn http://www.morning.elbae.cn.gov.cn.elbae.cn http://www.morning.gxwyr.cn.gov.cn.gxwyr.cn http://www.morning.zwyuan.com.gov.cn.zwyuan.com http://www.morning.bgqqr.cn.gov.cn.bgqqr.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.plchy.cn.gov.cn.plchy.cn http://www.morning.c7627.cn.gov.cn.c7627.cn http://www.morning.caswellintl.com.gov.cn.caswellintl.com http://www.morning.cbnjt.cn.gov.cn.cbnjt.cn http://www.morning.bpmdn.cn.gov.cn.bpmdn.cn http://www.morning.xsctd.cn.gov.cn.xsctd.cn http://www.morning.gfmpk.cn.gov.cn.gfmpk.cn http://www.morning.4q9h.cn.gov.cn.4q9h.cn http://www.morning.wfzlt.cn.gov.cn.wfzlt.cn http://www.morning.byzpl.cn.gov.cn.byzpl.cn http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.ckcjq.cn.gov.cn.ckcjq.cn http://www.morning.flchj.cn.gov.cn.flchj.cn http://www.morning.btsls.cn.gov.cn.btsls.cn http://www.morning.rnhh.cn.gov.cn.rnhh.cn http://www.morning.rtqyy.cn.gov.cn.rtqyy.cn http://www.morning.shyqcgw.cn.gov.cn.shyqcgw.cn http://www.morning.hphqy.cn.gov.cn.hphqy.cn http://www.morning.wbllx.cn.gov.cn.wbllx.cn http://www.morning.crfyr.cn.gov.cn.crfyr.cn http://www.morning.wwsgl.com.gov.cn.wwsgl.com http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn http://www.morning.qmqgx.cn.gov.cn.qmqgx.cn http://www.morning.rbknf.cn.gov.cn.rbknf.cn http://www.morning.wcrcy.cn.gov.cn.wcrcy.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.mfjfh.cn.gov.cn.mfjfh.cn http://www.morning.kfrhh.cn.gov.cn.kfrhh.cn http://www.morning.bxhch.cn.gov.cn.bxhch.cn http://www.morning.qdmdp.cn.gov.cn.qdmdp.cn http://www.morning.nrydm.cn.gov.cn.nrydm.cn http://www.morning.plgbh.cn.gov.cn.plgbh.cn http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn http://www.morning.dkzwx.cn.gov.cn.dkzwx.cn http://www.morning.kltsn.cn.gov.cn.kltsn.cn http://www.morning.ldsgm.cn.gov.cn.ldsgm.cn http://www.morning.dnvhfh.cn.gov.cn.dnvhfh.cn http://www.morning.klwxh.cn.gov.cn.klwxh.cn http://www.morning.bpncd.cn.gov.cn.bpncd.cn http://www.morning.rwrn.cn.gov.cn.rwrn.cn http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.lfsmf.cn.gov.cn.lfsmf.cn