日拱一卒无有尽,功不唐捐终入海。
前两周发了三篇SpringSecurity和一篇征文,这周打算写点简单有用易上手的文章,换换脑子,休息一下。前端
今天要写的是这篇:从LocalDateTime序列化来看全局一致性序列化体验
。java
这个标题看起来蛮不像人话的,有种挺官方的感受,我先给你们翻译翻译咱们的主题是什么:经过讲解LocalDateTime
的序列化从而引出整个项目中的全部序列化处理,并让他们保持一致。git
在咱们项目中通常存在着两种序列化,程序员
一个呢是SpringMVC
官方的序列化,也就是Spring
帮你作的序列化,好比你在一个接口上面打了一个ResponseBody
注解,SpringMVC
中的消息转换器会帮你作序列化。github
另外一个就是咱们项目内的序列化,本身定义的JsonUtil也好,仍是你引入的第三方JSON处理工具(好比FastJson
)也好,均可以说作是咱们项目内部的序列化。spring
这二者若是不同,有时候序列化出来的数据可能会出现结果不大同样的结果,为了防止这种状况,今天咱们就来探讨一下项目中的序列化。json
咱们先来举个例子,来看看若是序列化不一致会出现啥样的效果。api
@GetMapping("/api/anon") public ApiResult test01() { return ApiResult.ok("匿名访问成功"); }
这是一段很普通的访问接口,返回的结果以下:app
{ "code": 200, "msg": "请求成功", "data": { "请求成功": "匿名访问成功" }, "timestamp": "2020-07-19T23:07:07.738", "fail": false, "success": true }
这里你们只须要注意一下timestamp的序列化结果,timestamp是一个LocalDateTime
类型,在SpringMVC
中的消息转换器对LocalDateTime
作序列化的时候没有特殊处理,直接调用了LocalDateTime
的toString()方法,因此这个序列化结果中间有个T
。spring-boot
可是若是这里的序列化用了其余方案,可能这个序列化结果会是不同的体验,在个人项目中我也采用了Jackson
来作序列化(Spring
中也用的它),咱们能够看看咱们本身定义的一个JsonUtil对LocalDateTime
作序列化会是什么结果。
@Slf4j public class JacksonUtil { public static ObjectMapper objectMapper = new ObjectMapper(); /** * Java对象转JSON字符串 * * @param object * @return */ public static String toJsonString(Object object) { try { return objectMapper.writeValueAsString(object); } catch (JsonProcessingException e) { log.error("The JacksonUtil toJsonString is error : \n", e); throw new RuntimeException(); } } }
咱们序列化工具类长这样,和上面同样,咱们序列化一个ApiResult
看看会是什么结果:
{ "code": 400, "msg": "请求失败", "timestamp": { "month": "JULY", "year": 2020, "dayOfMonth": 19, "hour": 23, "minute": 25, "monthValue": 7, "nano": 596000000, "second": 2, "dayOfYear": 201, "dayOfWeek": "SUNDAY", "chronology": { "id": "ISO", "calendarType": "iso8601" } }, "fail": true, "success": false }
在Jackson
默认的ObjectMapper
下序列化出来的结果就是这个鬼样子,由于是序列化最后却是转化成字符串了,那这样的数据前端若是拿到了确定是不能正常转成时间类型的,
LocalDateTime
只是一个缩影,哪怕对于字符串,不一样的序列化配置也是有着不一样的影响,字符串里面可能会有转义字符,有引号,不一样的方案出来的结果多是不同的,
在实际项目中对第三方接口进行HTTP对接通常来讲都是须要的,其中传输过去的数据通常会通过咱们项目中JSON工具类的序列化为字符串以后再传输过去,若是序列化方案不一样可能会在序列化过程当中传过去的数据不是咱们想要的。
还有些接口是咱们直接往HttpServeletResponse
里面写数据,这种时候通常也是写JSON数据,好比:
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException { response.setHeader("Cache-Control", "no-cache"); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json"); response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.getWriter().println(JacksonUtil.toJsonString(ApiResult.fail(authException.getMessage()))); response.getWriter().flush(); }
这里我用工具类直接去序列化这个ApiResult
,传给前台的数据就会也出现上面例子中的状况,LocalDateTime
序列化结果不是咱们想要的。
因此在项目中的序列化和Spring
中的序列化保持一致仍是颇有必要的。
上面说过了项目中保持序列化的一致性的必要性(我认为是必要的哈哈)。
那咱们下面就能够说说若是去作这个一致性。
咱们知道,若是你想要在Spring
的序列化中将你返回的那个对象某个LocalDateTime
类型变量进行序列化的话,很简单,能够这样:
public class ApiResult implements Serializable { private static final Map<String, String> map = new HashMap<>(1); private int code; private String msg; private Object data; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime timestamp;
就很简单的在这个变量上面加一个JsonFormat
注解就ok了,但这样不是全局的,哪一个变量加哪一个变量就生效。
想作到全局生效,咱们须要在Spring
的配置去修改Spring
中使用的ObjectMapper
,了解Jackson的小伙伴应该都知道,序列化的各类配置都在配置在这个ObjectMapper
中的,不知道也不要紧,你如今知道了。
那么咱们能够经过去配置Spring
中的ObjectMapper
作到全局生效:
@Configuration public class JacksonConfig { @Bean public Jackson2ObjectMapperBuilderCustomizer customizer() { return builder -> { builder.locale(Locale.CHINA); builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss"); JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); builder.modules(javaTimeModule); }; } }
经过在Jackson2ObjectMapperBuilderCustomizer
之中加入一些序列化方案就能够达到这个效果,上文的代码就是作了这些操做,这样以后咱们再次访问最开始那个接口,就会出现以下效果:
{ "code": 200, "msg": "请求成功", "data": { "请求成功": "匿名访问成功" }, "timestamp": "2020-07-20 00:06:12", "fail": false, "success": true }
timestamp
中间那个T不存在了,由于咱们已经加入了LocalDateTime
的序列化方案了。
可是仅仅如此还不行,这只是作了LocalDateTime
的全局序列化,咱们还须要让本身的工具类也和Spring
的保持一致:
@Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); // 经过该方法对mapper对象进行设置,全部序列化的对象都将按改规则进行系列化 // Include.Include.ALWAYS 默认 // Include.NON_DEFAULT 属性为默认值不序列化 // Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的 // Include.NON_NULL 属性为NULL 不序列化 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 容许出现特殊字符和转义符 objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true); // 容许出现单引号 objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); /** * 将Long,BigInteger序列化的时候,转化为String */ // SimpleModule simpleModule = new SimpleModule(); // // simpleModule.addSerializer(Long.class, ToStringSerializer.instance); // simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance); // simpleModule.addSerializer(BigInteger.class, ToStringSerializer.instance); // // objectMapper.registerModule(simpleModule); // 将工具类中的 objectMapper 换为 Spring 中的 objectMapper JacksonUtil.objectMapper = objectMapper; return objectMapper; }
这段代码是紧跟上一步,对Jackson2ObjectMapperBuilder
builder出来的ObjectMapper
作一些操做,设置一系列本身想要的属性。
代码中注释那一块也是作一个序列化转换,若是你的项目中用到了比较长的LONG类型数字,可能会致使JS拿不到彻底的数字,由于java中的long类型要比JS的number类型长一点,这个时候你必需要转换成String给前台,它才能拿到正确的数字,若是你有须要能够打开这一段。
最后一句就是咱们比较关键的了,把builder出来的ObjectMapper
赋值给咱们工具类中的ObjectMapper
,这样的话它俩其实指向一个地址,也就是使用同一个对象进行序列化,所得出的结果固然就是相同的了。
今天的从LocalDateTime序列化探讨全局一致性序列化
就到这里了,但愿对你们有所帮助。
本文的代码我也放在以前的SpringSecruity
的demo中了,你们能够直接去里面搜索类名便可找到。
日拱一卒无有尽,功不唐捐终入海。
大家的每一个点赞收藏与评论都是对我知识输出的莫大确定,若是有文中有什么错误或者疑点或者对个人指教均可以在评论区下方留言,一块儿讨论。
我是耳朵,一个一直想作知识输出的伪文艺程序员,下期见。