refactor(date): 重构日期处理方式

- 移除实体类中的 @JSONField 和 @DateTimeFormat 注解
- 新增自定义日期转换器 DateStringArraysToListDateConverter 和 DateStringToDateConverter
- 在 ConfigurerAdapter 中注册自定义转换器
- 更新相关 DTO 类,移除冗余注解
This commit is contained in:
2025-05-15 21:09:25 +08:00
parent ffd7326250
commit 626f96dd00
16 changed files with 78 additions and 38 deletions
@@ -32,14 +32,10 @@ public class MyEntity extends MyObject {
@TableField(fill = FieldFill.INSERT) @TableField(fill = FieldFill.INSERT)
@ApiModelProperty(value = "创建时间: yyyy-MM-dd HH:mm:ss", hidden = true) @ApiModelProperty(value = "创建时间: yyyy-MM-dd HH:mm:ss", hidden = true)
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime; private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE) @TableField(fill = FieldFill.INSERT_UPDATE)
@ApiModelProperty(value = "更新时间: yyyy-MM-dd HH:mm:ss", hidden = true) @ApiModelProperty(value = "更新时间: yyyy-MM-dd HH:mm:ss", hidden = true)
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime; private Date updateTime;
/* 分组校验 */ /* 分组校验 */
@@ -12,7 +12,7 @@ import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.LastModifiedBy; import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.sql.Date; import java.util.Date;
/** /**
* 逻辑删除通用模型 * 逻辑删除通用模型
@@ -33,13 +33,9 @@ public class MyLogicEntity extends MyObject {
private String updateBy; private String updateBy;
@TableField(value = "create_time", fill = FieldFill.INSERT) @TableField(value = "create_time", fill = FieldFill.INSERT)
@ApiModelProperty(value = "创建时间", hidden = true) @ApiModelProperty(value = "创建时间", hidden = true)
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime; private Date createTime;
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE) @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
@ApiModelProperty(value = "更新时间", hidden = true) @ApiModelProperty(value = "更新时间", hidden = true)
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime; private Date updateTime;
@ApiModelProperty(value = "数据有效性", hidden = true) @ApiModelProperty(value = "数据有效性", hidden = true)
@TableLogic @TableLogic
@@ -48,7 +48,5 @@ public class QuartzLog extends MyObject {
@TableField(fill = FieldFill.INSERT) @TableField(fill = FieldFill.INSERT)
@ApiModelProperty(value = "创建时间", hidden = true) @ApiModelProperty(value = "创建时间", hidden = true)
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime; private Date createTime;
} }
@@ -46,7 +46,5 @@ public class QiniuContent extends MyObject {
@TableField(fill = FieldFill.INSERT_UPDATE) @TableField(fill = FieldFill.INSERT_UPDATE)
@ApiModelProperty(value = "创建或更新时间") @ApiModelProperty(value = "创建或更新时间")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime; private Date updateTime;
} }
@@ -1,4 +1,4 @@
package cn.odboy.core.framework.web; package cn.odboy.core.framework.web.config;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@@ -0,0 +1,32 @@
package cn.odboy.core.framework.web.converters;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Component
public class DateStringArraysToListDateConverter implements Converter<String[], List<Date>> {
private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public List<Date> convert(String[] source) {
if (source.length == 0) {
return Collections.emptyList();
}
return Arrays.stream(source)
.map(s -> {
try {
return sdf.parse(s);
} catch (ParseException e) {
throw new IllegalArgumentException("Invalid date string: " + s);
}
})
.collect(Collectors.toList());
}
}
@@ -0,0 +1,25 @@
package cn.odboy.core.framework.web.converters;
import cn.hutool.core.util.StrUtil;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class DateStringToDateConverter implements Converter<String, Date> {
private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public Date convert(String source) {
if (StrUtil.isBlank(source)) {
return null;
}
try {
return sdf.parse(source);
} catch (ParseException e) {
throw new IllegalArgumentException("Invalid date string: " + source);
}
}
}
@@ -1,12 +1,16 @@
package cn.odboy.core.framework.web; package cn.odboy.core.framework.web.core;
import cn.odboy.core.framework.properties.AppProperties; import cn.odboy.core.framework.properties.AppProperties;
import cn.odboy.core.framework.properties.dto.FileUploadSettingModel; import cn.odboy.core.framework.properties.dto.FileUploadSettingModel;
import cn.odboy.core.framework.web.converters.DateStringArraysToListDateConverter;
import cn.odboy.core.framework.web.converters.DateStringToDateConverter;
import com.alibaba.fastjson2.JSONWriter; import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.support.config.FastJsonConfig; import com.alibaba.fastjson2.support.config.FastJsonConfig;
import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter; import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter;
@@ -22,16 +26,14 @@ import java.util.List;
@Configuration @Configuration
@EnableWebMvc @EnableWebMvc
@RequiredArgsConstructor
public class ConfigurerAdapter implements WebMvcConfigurer { public class ConfigurerAdapter implements WebMvcConfigurer {
/** /**
* 文件配置 * 文件配置
*/ */
private final AppProperties properties; private final AppProperties appProperties;
private final DateStringArraysToListDateConverter dateStringArraysToListDateConverter;
public ConfigurerAdapter(AppProperties properties) { private final DateStringToDateConverter dateStringToDateConverter;
this.properties = properties;
}
@Bean @Bean
public CorsFilter corsFilter() { public CorsFilter corsFilter() {
@@ -47,7 +49,7 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
@Override @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
FileUploadSettingModel.ElPath path = properties.getFile().getPath(); FileUploadSettingModel.ElPath path = appProperties.getFile().getPath();
String avatarUtl = "file:" + path.getAvatar().replace("\\", "/"); String avatarUtl = "file:" + path.getAvatar().replace("\\", "/");
String pathUtl = "file:" + path.getPath().replace("\\", "/"); String pathUtl = "file:" + path.getPath().replace("\\", "/");
registry.addResourceHandler("/avatar/**").addResourceLocations(avatarUtl).setCachePeriod(0); registry.addResourceHandler("/avatar/**").addResourceLocations(avatarUtl).setCachePeriod(0);
@@ -87,4 +89,13 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
// 将 FastJsonHttpMessageConverter 添加到列表末尾 // 将 FastJsonHttpMessageConverter 添加到列表末尾
converters.add(fastJsonConverter); converters.add(fastJsonConverter);
} }
/**
* 自定义时间转换器
*/
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(dateStringArraysToListDateConverter);
registry.addConverter(dateStringToDateConverter);
}
} }
@@ -26,7 +26,5 @@ public class QueryDeptRequest {
private Boolean pidIsNull; private Boolean pidIsNull;
@ApiModelProperty(value = "创建时间") @ApiModelProperty(value = "创建时间")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private List<Date> createTime; private List<Date> createTime;
} }
@@ -20,8 +20,6 @@ public class QueryJobRequest {
private Boolean enabled; private Boolean enabled;
@ApiModelProperty(value = "创建时间") @ApiModelProperty(value = "创建时间")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private List<Date> createTime; private List<Date> createTime;
@ApiModelProperty(value = "页码", example = "1") @ApiModelProperty(value = "页码", example = "1")
@@ -15,8 +15,6 @@ public class QueryMenuRequest {
private String blurry; private String blurry;
@ApiModelProperty(value = "创建时间") @ApiModelProperty(value = "创建时间")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private List<Date> createTime; private List<Date> createTime;
@ApiModelProperty(value = "PID为空查询") @ApiModelProperty(value = "PID为空查询")
@@ -18,8 +18,6 @@ public class QueryQuartzJobRequest {
private Boolean isSuccess; private Boolean isSuccess;
@ApiModelProperty(value = "创建时间") @ApiModelProperty(value = "创建时间")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private List<Date> createTime; private List<Date> createTime;
@ApiModelProperty(value = "页码", example = "1") @ApiModelProperty(value = "页码", example = "1")
@@ -15,8 +15,6 @@ public class QueryRoleRequest {
private String blurry; private String blurry;
@ApiModelProperty(value = "创建时间") @ApiModelProperty(value = "创建时间")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private List<Date> createTime; private List<Date> createTime;
@ApiModelProperty(value = "页码", example = "1") @ApiModelProperty(value = "页码", example = "1")
@@ -31,8 +31,6 @@ public class QueryUserRequest extends MyObject {
private Long deptId; private Long deptId;
@ApiModelProperty(value = "创建时间") @ApiModelProperty(value = "创建时间")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private List<Date> createTime; private List<Date> createTime;
@ApiModelProperty(value = "页码", example = "1") @ApiModelProperty(value = "页码", example = "1")
@@ -15,8 +15,6 @@ public class QueryLocalStorageRequest {
private String blurry; private String blurry;
@ApiModelProperty(value = "创建时间") @ApiModelProperty(value = "创建时间")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private List<Date> createTime; private List<Date> createTime;
@ApiModelProperty(value = "页码", example = "1") @ApiModelProperty(value = "页码", example = "1")
@@ -15,8 +15,6 @@ public class QueryQiniuRequest {
private String key; private String key;
@ApiModelProperty(value = "创建时间") @ApiModelProperty(value = "创建时间")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private List<Date> createTime; private List<Date> createTime;
@ApiModelProperty(value = "页码", example = "1") @ApiModelProperty(value = "页码", example = "1")