Commit 63f33c44 by 王璐琦

feat(request): 添加日期格式化注解以规范时间字段序列化

- 在submissionDate字段上添加@DateTimeFormat注解用于前端参数接收格式化
- 添加@JsonFormat注解配置序列化为指定时间格式及时区
- 引入DateTimeFormat和JsonFormat相关依赖包
- 保证日期时间格式统一,避免序列化和反序列化错误
1 parent 0f4d0199
......@@ -4,11 +4,13 @@ import cn.com.glsx.dj.daily.report.dto.DailyReportCarBrandDTO;
import cn.com.glsx.dj.daily.report.dto.DailyReportFieldDataDTO;
import cn.com.glsx.dj.daily.report.dto.DailyReportSubformDataDTO;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.rop.AbstractRopRequest;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;
import javax.validation.constraints.NotNull;
import java.util.Date;
......@@ -62,6 +64,8 @@ public class DynamicDailyReportSaveRequest extends AbstractRopRequest {
@ApiModelProperty(name = "submissionStatus", notes = "上报状态(1:已上报,2:未上报)")
private Byte submissionStatus;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty(name = "submissionDate", notes = "上报时间")
private Date submissionDate;
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议(仅限 Top 3)

    1. 序列化框架潜在冲突(功能正确性/可维护性)

    • 问题描述:代码顶部引入了 com.alibaba.fastjson.JSONObject,但在新增的日期格式化中却使用了 Jackson 的注解 @JsonFormat。如果当前项目(或全局 Web MVC 配置)使用的是 Fastjson 作为默认的 HTTP 消息转换器(HttpMessageConverter),那么 Jackson 的 @JsonFormat 注解将会完全失效。
    • 优化建议:请确认项目全局使用的 JSON 序列化框架。如果主框架是 Fastjson,应将 @JsonFormat 替换为 Fastjson 的 @JSONField(format = "yyyy-MM-dd HH:mm:ss");如果是 Jackson,建议清理无用的 Fastjson 依赖,保持统一,避免混合使用导致维护混乱。

    2. 违反阿里巴巴开发手册关于时间类型的规范(最佳实践)

    • 问题描述:字段 submissionDate 使用了 java.util.Date 类型。根据《阿里巴巴 Java 开发手册》日期时间规约,强制要求不要在程序中使用 java.util.Date,因为它存在线程安全问题(如配合 SimpleDateFormat 时)且 API 设计存在缺陷。
    • 优化建议:建议将 java.util.Date 替换为 Java 8 引入的 java.time.LocalDateTime。替换后,Jackson 序列化注解可保持不变(需配合 JavaTimeModule),这不仅线程安全,还能提升代码的现代化和健壮性。

    3. 时区硬编码降低了系统的扩展性(可维护性)

    • 问题描述:@JsonFormat 中硬编码了 timezone = "GMT+8"。虽然当前业务可能仅面向国内,但这是一种局部的硬编码做法。如果未来系统需要支持多时区或出海,逐个修改 DTO 字段的工作量极大且容易遗漏。
    • 优化建议:建议移除注解中的 timezone 属性,将时间格式和时区配置统一沉淀到 Spring Boot 的全局配置文件中(如 spring.jackson.time-zone=GMT+8 和 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss)。仅在个别字段有特殊格式要求时,才使用注解进行局部覆盖。

    二、评分明细

    • 1. 功能实现的正确性与健壮性(36/40分):注解使用语法正确,但存在严重的 Fastjson 与 Jackson 混用隐患,若项目默认使用 Fastjson,该功能将无法生效,扣除4分。
    • 2. 安全性与潜在风险(30/30分):仅涉及格式化注解的添加,无 SQL 注入、XSS、越权等安全风险。
    • 3. 最佳实践与可维护性(14/20分):使用了过时的 java.util.Date 类型,违反了阿里规约;时区硬编码不利于统一维护;JSON 库依赖混用。扣除6分。
    • 4. 性能与资源利用(5/5分):注解的添加对系统性能影响微乎其微,资源利用合理。
    • 5. 提交信息质量(5/5分):Commit Message 符合 Angular 规范(feat(request): ...),结构清晰,说明了修改内容、目的及影响,质量极佳。

    总分:90分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!