From 2ec90aa3f47522464ee914046c35757d6bc47011 Mon Sep 17 00:00:00 2001 From: zhangli <123879394@qq.com> Date: Wed, 30 Jul 2025 01:35:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=97=B6=E9=97=B4=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../order/config/LocalDateTimeConfig.java | 53 ------------------- .../com/sczx/order/config/WebMvcConfig.java | 31 +++++++++++ .../java/com/sczx/order/dto/OrderDTO.java | 8 +++ .../java/com/sczx/order/dto/OrderSubDTO.java | 3 ++ .../java/com/sczx/order/po/OrderMainPO.java | 13 ++--- 5 files changed, 47 insertions(+), 61 deletions(-) delete mode 100644 src/main/java/com/sczx/order/config/LocalDateTimeConfig.java diff --git a/src/main/java/com/sczx/order/config/LocalDateTimeConfig.java b/src/main/java/com/sczx/order/config/LocalDateTimeConfig.java deleted file mode 100644 index ad7888a..0000000 --- a/src/main/java/com/sczx/order/config/LocalDateTimeConfig.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.sczx.order.config; - -import com.fasterxml.jackson.databind.Module; -import com.fasterxml.jackson.databind.module.SimpleModule; -import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; -import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; -import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; - -import java.text.SimpleDateFormat; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.TimeZone; - -@Configuration -public class LocalDateTimeConfig { - - @Bean - @Primary - public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { - return new Jackson2ObjectMapperBuilder() - .dateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")) - .timeZone(TimeZone.getTimeZone("GMT+8")); - } - - /** - * LocalDate 类型序列化 - */ - @Bean - @Primary - public LocalDateTimeSerializer localDateTimeSerializer() { - return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); - } - - /** - * LocalDate 类型反序列化 - */ - @Bean - @Primary - public LocalDateTimeDeserializer localDateTimeDeserializer() { - return new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); - } - - @Bean - public Module localDateTimeModule() { - SimpleModule module = new SimpleModule(); - module.addSerializer(LocalDateTime.class, localDateTimeSerializer()); - module.addDeserializer(LocalDateTime.class, localDateTimeDeserializer()); - return module; - } -} diff --git a/src/main/java/com/sczx/order/config/WebMvcConfig.java b/src/main/java/com/sczx/order/config/WebMvcConfig.java index 7c57e32..c6639a1 100644 --- a/src/main/java/com/sczx/order/config/WebMvcConfig.java +++ b/src/main/java/com/sczx/order/config/WebMvcConfig.java @@ -1,11 +1,22 @@ package com.sczx.order.config; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.List; +import java.util.TimeZone; @Configuration public class WebMvcConfig implements WebMvcConfigurer { @@ -14,5 +25,25 @@ public class WebMvcConfig implements WebMvcConfigurer { public void configureMessageConverters(List> converters) { // 确保使用 Jackson 而不是 FastJSON converters.removeIf(converter -> converter instanceof FastJsonHttpMessageConverter); + + // 确保Jackson转换器在第一位 + converters.add(0, new MappingJackson2HttpMessageConverter(objectMapper())); + } + + @Bean + @Primary + public ObjectMapper objectMapper() { + ObjectMapper mapper = new ObjectMapper(); + + // 配置LocalDateTime序列化 + JavaTimeModule javaTimeModule = new JavaTimeModule(); + javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); + javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); + + mapper.registerModule(javaTimeModule); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + mapper.setTimeZone(TimeZone.getTimeZone("GMT+8")); + + return mapper; } } diff --git a/src/main/java/com/sczx/order/dto/OrderDTO.java b/src/main/java/com/sczx/order/dto/OrderDTO.java index 39a0662..4f55fe4 100644 --- a/src/main/java/com/sczx/order/dto/OrderDTO.java +++ b/src/main/java/com/sczx/order/dto/OrderDTO.java @@ -1,5 +1,6 @@ package com.sczx.order.dto; +import com.fasterxml.jackson.annotation.JsonFormat; import com.sczx.order.thirdpart.dto.CarModelSimpleDTO; import com.sczx.order.thirdpart.dto.CompanyStoreDTO; import io.swagger.annotations.ApiModel; @@ -82,18 +83,23 @@ public class OrderDTO { @ApiModelProperty("是否开通代扣") private Boolean isAutoDeduct; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @ApiModelProperty("首次下单时间") private LocalDateTime firstOrderTime; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @ApiModelProperty("开始计费时间") private LocalDateTime startRentTime; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @ApiModelProperty("还车时间") private LocalDateTime endRentTime; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @ApiModelProperty("申请还车时间") private LocalDateTime reqEndRentTime; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @ApiModelProperty("实际还车时间") private LocalDateTime actEndRentTime; @@ -112,9 +118,11 @@ public class OrderDTO { @ApiModelProperty("租电套餐id") private Long rentBatteyRuleId; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @ApiModelProperty("创建时间") private LocalDateTime createTime; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @ApiModelProperty("更新时间") private LocalDateTime updateTime; diff --git a/src/main/java/com/sczx/order/dto/OrderSubDTO.java b/src/main/java/com/sczx/order/dto/OrderSubDTO.java index 1073268..2b5554d 100644 --- a/src/main/java/com/sczx/order/dto/OrderSubDTO.java +++ b/src/main/java/com/sczx/order/dto/OrderSubDTO.java @@ -1,5 +1,6 @@ package com.sczx.order.dto; +import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -32,9 +33,11 @@ public class OrderSubDTO { @ApiModelProperty("车架/电池编号") private String vinBatteryNo; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @ApiModelProperty("订单产生时间") private LocalDateTime createdAt; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @ApiModelProperty("支付ID") private String paymentId; diff --git a/src/main/java/com/sczx/order/po/OrderMainPO.java b/src/main/java/com/sczx/order/po/OrderMainPO.java index 3841d18..e9280d9 100644 --- a/src/main/java/com/sczx/order/po/OrderMainPO.java +++ b/src/main/java/com/sczx/order/po/OrderMainPO.java @@ -3,7 +3,6 @@ package com.sczx.order.po; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; -import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Getter; @@ -88,27 +87,27 @@ public class OrderMainPO implements Serializable { private Boolean isAutoDeduct; - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @ApiModelProperty("首次下单时间") private LocalDateTime firstOrderTime; - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @ApiModelProperty("开始计费时间") private LocalDateTime startRentTime; - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @ApiModelProperty("还车时间") private LocalDateTime endRentTime; - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @ApiModelProperty("申请还车时间") private LocalDateTime reqEndRentTime; - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @ApiModelProperty("实际还车时间") private LocalDateTime actEndRentTime; @@ -131,12 +130,10 @@ public class OrderMainPO implements Serializable { private String delFlag; - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @ApiModelProperty("创建时间") private LocalDateTime createTime; - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @ApiModelProperty("更新时间") private LocalDateTime updateTime;