关于 localDateTime 返回前端的格式问题

2019-07-15 16:33:37 +08:00
 scyllaEo
"createTime": {
        "dayOfMonth": 15,
        "dayOfWeek": "MONDAY",
        "dayOfYear": 196,
        "month": "JULY",
        "year": 2019,
        "monthValue": 7,
        "hour": 14,
        "minute": 50,
        "nano": 0,
        "second": 40,
        "chronology": {
          "id": "ISO",
          "calendarType": "iso8601"
        }
      }

上述就是返回的格式。请问这是为啥,已经在 WebMvcConfigurer 配置了 pattern,并且注册到容器了。

@Bean
public LocalDateTimeSerializer localDateTimeDeserializer() {
   return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
//注册
builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());

有没有人解答一下

11673 次点击
所在节点    Java
25 条回复
wu67
2019-07-15 16:54:18 +08:00
exm? 你的意思是要让前端显示 yyyy-MM-dd HH:mm:ss 这样? 直接把 long 丢过去让前端处理呀
telami
2019-07-15 17:01:12 +08:00
问题有很多种解决办法,没看懂你的选择
honeycomb
2019-07-15 17:05:09 +08:00
@wu67 他要让 mvc 的序列化机制直接把 LocalDateTime 转成 String
broadliyn
2019-07-15 17:09:59 +08:00
1. 看你前段返回时候序列化是用的 spring boot 自带的,还是自己另外建的 JsonUtil
2. builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer()); 这个 builder 的来源是 1 里的哪一个?
yinzhili
2019-07-15 17:10:36 +08:00
字段上面加注解:
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonSerialize(using = LocalDateTimeSerializer.class)
scyllaEo
2019-07-15 17:16:46 +08:00
@yinzhili 要做统一处理,这样一个一个加就有点麻烦
scyllaEo
2019-07-15 17:17:19 +08:00
@broadliyn ``` @Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {

return builder -> {
builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
};
```
cyheng
2019-07-15 17:19:36 +08:00
objectMapper addDeserializer 试一下?
xfriday
2019-07-15 17:39:15 +08:00
用 RFC3339 ("2006-01-02T15:04:05Z07:00"),带上时区信息,方便前端做国际化处理
broadliyn
2019-07-15 17:40:56 +08:00
看一下,MappingJackson2HttpMessageConverter 和 JacksonAutoConfiguration
目测是配置没有生效。
npe
2019-07-15 17:43:46 +08:00
@Configuration
public class ApiConfiguration implements WebMvcConfigurer {


@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8);
}

private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> {
builder.simpleDateFormat(DATE_TIME_FORMAT);
builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)));
builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
};
}


}
npe
2019-07-15 17:44:07 +08:00
``` Java

@Configuration
public class ApiConfiguration implements WebMvcConfigurer {


@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8);
}

private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> {
builder.simpleDateFormat(DATE_TIME_FORMAT);
builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)));
builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
};
}


}

```
liuawei
2019-07-15 18:43:49 +08:00
配置文件里面配置这个看下
spring.mvc.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
liuawei
2019-07-15 18:46:09 +08:00
或者重新 configureMessageConverters 这个方法
FleyX
2019-07-15 18:48:26 +08:00
关于时间的前后端交互问题,从来都是用的时间戳。客户端可以格式化为任意格式。也不存在时区转换问题。
zhady009
2019-07-15 19:15:24 +08:00
@Configuration
public class LocalDateTimeFormatter {

@Bean
public LocalDateTimeSerializer localDateTimeDeserializer() {
return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DateFormat.NORMAL_DATE));
}

@Bean
public LocalDateSerializer localDateDeserializer() {
return new LocalDateSerializer(DateTimeFormatter.ofPattern(DateFormat.SHORT_DATE));
}

@Bean
public LocalTimeSerializer localTimeDeserializer() {
return new LocalTimeSerializer(DateTimeFormatter.ofPattern(DateFormat.NORMAL_TIME));
}

@Bean
public Jackson2ObjectMapperBuilderCustomizer localDateTimeDeserializerCustomizer() {
return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
}

@Bean
public Jackson2ObjectMapperBuilderCustomizer localDateDeserializerCustomizer() {
return builder -> builder.serializerByType(LocalDate.class, localDateDeserializer());
}

@Bean
public Jackson2ObjectMapperBuilderCustomizer localTimeDeserializerCustomizer() {
return builder -> builder.serializerByType(LocalTime.class, localTimeDeserializer());
}

}
alangz
2019-07-15 19:23:21 +08:00
如果用的是 Jackson,你需要配置加载 jsr310 的 JavaTimeModule 模块儿,并配置关闭 WRITE_DATES_AS_TIMESTAMPS。
alangz
2019-07-15 19:24:20 +08:00
补充下,看到你这个返回结果就是没有添加这个模块儿。
qiyuey
2019-07-15 19:45:04 +08:00
建议走 ISO 标准格式,就是带 T 的那个
WispZhan
2019-07-15 20:06:04 +08:00
请你返回时间戳谢谢。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/583131

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX