问题描述:实体类实现了Serializable接口,实体类中有一个LocalDateTime类型的属性,用@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")注解进行修饰,且application.properties中配置了

spring.jackson.serialization.write_dates_as_timestamps=false,但是前端收到的对象中的时间是时间戳格式

最终百度之后在config中的Mybaitis配置文件中添加了一个Bean

@Bean
public ObjectMapper serializingObjectMapper() {
  ObjectMapper objectMapper = new ObjectMapper();
  objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
  objectMapper.registerModule(new JavaTimeModule());
  return objectMapper;
}

返回了时间格式的字符串

但是后面梳理发现在config目录下的SpringConfig中有下面一段

@Bean
	public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
		return builder -> {
			builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer());
			builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer());
		};
	}



	/**
	 * 序列化
	 */
	public static class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
		@Override
		public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers)
				throws IOException {
			if (value != null) {
				long timestamp = value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
				gen.writeNumber(timestamp);
			}
		}
	}

	/**
	 * 反序列化
	 */
	public static class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
		@Override
		public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext)
				throws IOException {
			long timestamp = p.getValueAsLong();
			if (timestamp > 0) {
				return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
			} else {
				return null;
			}
		}
	}

去掉我写的Bean和这个序列化的这个bean就正确了

分析原因如下:涉及spring配置文件的加载顺序及ObjectMapper应用

首先    分析为什么没有加serializingObjectMapper之前配置文件中的配置没有生效,因为按照配置文件的加载顺序,先加载根目录下的application.properties,然后加载config目录下的其他文件,SpringConfig配置文件中的序列化与反序列化处理的LocalDateTime是返回时间戳,对application.properties文件中的配置进行了覆盖

其次加入serializingObjectMapper之后,应该是先加载我写的MybatisConfig这个配置,再加载SpringConfig中这个配置,为什么没有覆盖我的配置

原因是

SpringConfig中配置的是默认的,我写在MybatisConfig中的是自定义的,优先级会高一点,不会 对其进行覆盖

其实通过debug也可以发现

没有执行到if判断这一行项目就开始运行了

注:可以根据下面的方法进行配置文件加载优先级的测试

@Bean

public TomcatServletWebServerFactory servletContainer(){undefined

return new TomcatServletWebServerFactory(8081) ;

}

 

GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me> Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 4 天前
d23291ba * add a ci step for Json_Diagnostic_Positions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * Update ci.cmake to address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typo in the comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typos in ci.cmake Signed-off-by: Harinath Nampally <harinath922@gmail.com> * invoke the new ci step from ubuntu.yml Signed-off-by: Harinath Nampally <harinath922@gmail.com> * issue4561 - use diagnostic positions for exceptions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci check failures for unit-diagnostic-postions.cpp Signed-off-by: Harinath Nampally <harinath922@gmail.com> * improvements based on review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix const correctness string Signed-off-by: Harinath Nampally <harinath922@gmail.com> * further refinements based on reviews Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add one more test case for full coverage Signed-off-by: Harinath Nampally <harinath922@gmail.com> * ci check fix - add const Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add unit tests for json_diagnostic_postions only Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_diagnostics Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_build_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> --------- Signed-off-by: Harinath Nampally <harinath922@gmail.com> 4 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐