Java LocalDateTime以数组格式表示
我在Java8中使用SpringBoot2.2.6和Jackson 2.10.3。我在整个项目中使用localdatetime对象。Jackson无法正确解析LocalDateTime(或者可能是它的默认格式)并以如下所示的数组格式发送json响应中的日期Java LocalDateTime以数组格式表示,java,spring-boot,objectmapper,jackson2,Java,Spring Boot,Objectmapper,Jackson2,我在Java8中使用SpringBoot2.2.6和Jackson 2.10.3。我在整个项目中使用localdatetime对象。Jackson无法正确解析LocalDateTime(或者可能是它的默认格式)并以如下所示的数组格式发送json响应中的日期 "createdDate": [ 2020, 8, 31, 0, 0,
"createdDate": [
2020,
8,
31,
0,
0,
0,
80000000
]
如中所述,SpringBoot2在类路径上已经有默认的jackson-datatype-jsr310:2.10.3。我想在整个项目中用json表示日期为2020-03-31:00。第一个解决方案在上述链接中不起作用。在此之后,我尝试了@JsonSerialize注释,它可以工作,但我不想应用于每个类。所以也尝试覆盖对象映射器,但没有成功
@Primary
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
SimpleModule module = new SimpleModule("my custom date serializer");
module.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer());
mapper.registerModule(module);
return mapper;
}
还尝试自定义Jackson2ObjectMapperBuilder,但仍然以数组格式显示日期
@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
SimpleModule module = new SimpleModule("my custom date serializer");
module.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer());
builder.modulesToInstall(modules)
return builder;
}
也尝试使用Jackson2ObjectMapperBuilderCustomizer
@Configuration
public class JacksonConfiguration {
@Primary
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> {
builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
};
}
}
@RestConroller
@RequestMapping("/user")
UserController {
User getUser(){
User user = new User()
user.createdDate = LocalDateTime.now();
return user;
}
}
controller
@Configuration
public class JacksonConfiguration {
@Primary
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> {
builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
};
}
}
@RestConroller
@RequestMapping("/user")
UserController {
User getUser(){
User user = new User()
user.createdDate = LocalDateTime.now();
return user;
}
}
在全球层面上,我能做些什么,所以项目中的每个日期都将以字符串格式序列化,比如2020-09-01
任何建议都会有帮助。创建一个bean:
添加属性
spring.jackson.serialization.write_dates\u as_timestamps=false
到应用程序.properties
,或者您也可以在Jackson2ObjectMapperBuilderCustomizer
中以编程方式执行此操作。我的一个cors类拥有@EnableWebMVC
注释。这将禁用spring引导自动配置,并覆盖映射器配置。这不允许您覆盖对象映射器的配置。通过删除@EnableMVC
注释,现在可以正常工作了。在SpringBoot2中,我们不需要进行任何与日期序列化明确相关的配置,它将自动以“yyyy-mm-dd”格式解析java 8日期。@PrabjotSingh听起来您是从Spring无法控制的地方序列化的。发布你的序列化代码。我已经编辑了这个问题。添加了Jackson2ObjectMapperBuilderCustomizer代码和控制器代码。仍然存在相同的问题,尝试使用prop文件和java配置。@PrabjotSingh抱歉,我不知道如何帮助您。自己调试你的代码。谢谢你的帮助,我得到了答案。我把它贴出来了。在迷路寻找答案后,谢谢你分享。工作得很有魅力!