当前位置: 首页 > news >正文

济南网站开发招聘匿名留言墙 wordpress

济南网站开发招聘,匿名留言墙 wordpress,搜索引擎优化seo是什么,抖音小程序定制开发在部分场景中,后台的时间属性用的不是Date或Long,而是Instant,Java8引入的一个精度极高的时间类型,可以精确到纳秒,但实际使用的时候不需要这么高的精确度,通常到毫秒就可以了。 而在前后端传参的时候需要…

在部分场景中,后台的时间属性用的不是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();@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 JsonSerializer<Instant> {@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 JsonDeserializer<Instant> {@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 Converter<String, Instant> instantConvert() {// 不能替换为lambda表达式return new Converter<String, 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);}@Data@NoArgsConstructor@AllArgsConstructorstatic class DemoVO {private Instant instant;}
}
http://www.yayakq.cn/news/673991/

相关文章:

  • 做推广比较好的网站有哪些手机管理网站模板下载安装
  • 郑州网站开发培训用帝国cms做企业网站
  • 小地方做外卖网站怎样建站属于什么行业
  • 专做机酒的网站空间坐标系做图网站
  • 企业是做app还是做网站公众号编辑 wordpress
  • 网站编辑的工作职能有哪些wordpress微信公众号企业版
  • 天津城市建设大学网站怎么建立网站数据库连接
  • 哪方面的网站动漫设计与制作专业学什么
  • 网站设计好学吗网站建设与应用
  • 潍坊建站公司搜索引擎seo如何赚钱
  • 北京网站建设开发服务器 网站建设 过程
  • 员工管理网站模板软文营销的特点有哪些
  • 网站通栏代码温州营销推广公司
  • 电商网站什么要求高网络营销课程思维导图
  • 做网站创业需要注册公司吗flask做网站工具
  • 微信分销网站建设多少钱wordpress 显示视频
  • wordpress 滑块验证码排名优化软件
  • 自己怎么开网站做菠菜网站好赚吗
  • 做网站像素大小交互设计英文
  • 排行榜网站易点科技
  • 商务平台网站建设合同网站如何申请微信支付接口
  • 电线电缆做销售哪个网站好thinkphp手机网站制作
  • 网站登录窗口怎么做做游戏制作 网站
  • 网站移动适配怎么做网站数据库连接出错
  • 文案推广发布网站大全建设银行网站 购买外汇
  • 做企业网站电话销售话术小程序打不开什么原因
  • 网站建设 重点免费企业邮箱哪家好
  • 自动点击器永久免费版广州seo外包
  • 公司做网站的费用会计分录园林专业设计学习网站
  • 网站建设与制作区别网站栏目结构设计