增加时间格式配置

This commit is contained in:
2025-07-30 01:35:29 +08:00
parent 8a024662f7
commit 2ec90aa3f4
5 changed files with 47 additions and 61 deletions

View File

@ -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;
}
}

View File

@ -1,11 +1,22 @@
package com.sczx.order.config; package com.sczx.order.config;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; 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.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List; import java.util.List;
import java.util.TimeZone;
@Configuration @Configuration
public class WebMvcConfig implements WebMvcConfigurer { public class WebMvcConfig implements WebMvcConfigurer {
@ -14,5 +25,25 @@ public class WebMvcConfig implements WebMvcConfigurer {
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// 确保使用 Jackson 而不是 FastJSON // 确保使用 Jackson 而不是 FastJSON
converters.removeIf(converter -> converter instanceof FastJsonHttpMessageConverter); 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;
} }
} }

View File

@ -1,5 +1,6 @@
package com.sczx.order.dto; package com.sczx.order.dto;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.sczx.order.thirdpart.dto.CarModelSimpleDTO; import com.sczx.order.thirdpart.dto.CarModelSimpleDTO;
import com.sczx.order.thirdpart.dto.CompanyStoreDTO; import com.sczx.order.thirdpart.dto.CompanyStoreDTO;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
@ -82,18 +83,23 @@ public class OrderDTO {
@ApiModelProperty("是否开通代扣") @ApiModelProperty("是否开通代扣")
private Boolean isAutoDeduct; private Boolean isAutoDeduct;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("首次下单时间") @ApiModelProperty("首次下单时间")
private LocalDateTime firstOrderTime; private LocalDateTime firstOrderTime;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("开始计费时间") @ApiModelProperty("开始计费时间")
private LocalDateTime startRentTime; private LocalDateTime startRentTime;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("还车时间") @ApiModelProperty("还车时间")
private LocalDateTime endRentTime; private LocalDateTime endRentTime;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("申请还车时间") @ApiModelProperty("申请还车时间")
private LocalDateTime reqEndRentTime; private LocalDateTime reqEndRentTime;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("实际还车时间") @ApiModelProperty("实际还车时间")
private LocalDateTime actEndRentTime; private LocalDateTime actEndRentTime;
@ -112,9 +118,11 @@ public class OrderDTO {
@ApiModelProperty("租电套餐id") @ApiModelProperty("租电套餐id")
private Long rentBatteyRuleId; private Long rentBatteyRuleId;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("创建时间") @ApiModelProperty("创建时间")
private LocalDateTime createTime; private LocalDateTime createTime;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("更新时间") @ApiModelProperty("更新时间")
private LocalDateTime updateTime; private LocalDateTime updateTime;

View File

@ -1,5 +1,6 @@
package com.sczx.order.dto; package com.sczx.order.dto;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
@ -32,9 +33,11 @@ public class OrderSubDTO {
@ApiModelProperty("车架/电池编号") @ApiModelProperty("车架/电池编号")
private String vinBatteryNo; private String vinBatteryNo;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("订单产生时间") @ApiModelProperty("订单产生时间")
private LocalDateTime createdAt; private LocalDateTime createdAt;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("支付ID") @ApiModelProperty("支付ID")
private String paymentId; private String paymentId;

View File

@ -3,7 +3,6 @@ package com.sczx.order.po;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Getter; import lombok.Getter;
@ -88,27 +87,27 @@ public class OrderMainPO implements Serializable {
private Boolean isAutoDeduct; private Boolean isAutoDeduct;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("首次下单时间") @ApiModelProperty("首次下单时间")
private LocalDateTime firstOrderTime; private LocalDateTime firstOrderTime;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("开始计费时间") @ApiModelProperty("开始计费时间")
private LocalDateTime startRentTime; private LocalDateTime startRentTime;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("还车时间") @ApiModelProperty("还车时间")
private LocalDateTime endRentTime; private LocalDateTime endRentTime;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("申请还车时间") @ApiModelProperty("申请还车时间")
private LocalDateTime reqEndRentTime; private LocalDateTime reqEndRentTime;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("实际还车时间") @ApiModelProperty("实际还车时间")
private LocalDateTime actEndRentTime; private LocalDateTime actEndRentTime;
@ -131,12 +130,10 @@ public class OrderMainPO implements Serializable {
private String delFlag; private String delFlag;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("创建时间") @ApiModelProperty("创建时间")
private LocalDateTime createTime; private LocalDateTime createTime;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("更新时间") @ApiModelProperty("更新时间")
private LocalDateTime updateTime; private LocalDateTime updateTime;