[SpringBoot]日期类型的参数GET和POST传参依赖不同的注解

PUT请求执行一个编辑数据操作,从服务端直接返回报错信息:

JSON parse error: Cannot deserialize value of type `java.util.Date` from String \"2025-08-12\": not a valid representation (error: Failed to parse Date value '2025-08-12': Unparseable date: \"2025-08-12\"); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String \"2025-08-12\": not a valid representation (error: Failed to parse Date value '2025-08-12': Unparseable date: \"2025-08-12\")\n at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 105] (through reference chain: com.alumni.domain.bo.ReservationBo[\"returnDate\"])",

报错前参数实体代码:

    @NotNull(message = "返校日期不能为空", groups = { AddGroup.class, EditGroup.class, MiniappGroup.class })
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date returnDate;

报错后修改为完全正确:

    @NotNull(message = "返校日期不能为空", groups = { AddGroup.class, EditGroup.class, MiniappGroup.class })
    @JsonFormat( shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date returnDate;

分析:
Successfully resolved the JSON deserialization error for the reservation edit functionality.

Problem Analysis: The error occurred when editing reservations via the PUT endpoint ( /edit ) because Jackson could not deserialize the JSON date string "2025-08-12" to a Date object. The error message indicated that the returnDate field in XyhReservationBo was missing proper JSON format configuration.

Root Cause: The @JsonFormat annotation for the returnDate field had been commented out, leaving only the @DateTimeFormat annotation. While @DateTimeFormat handles URL parameter binding, @JsonFormat is required for JSON request body deserialization.

Solution Implemented: Restored the complete date handling configuration in ReservationBo.java :

1.    Uncommented @JsonFormat annotation : Restored @JsonFormat( shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "GMT+8") for the returnDate field
2.    Added missing import : Added import com.fasterxml.jackson.annotation.JsonFormat; to resolve compilation errors
3.    Maintained dual annotation approach : Kept both @JsonFormat and @DateTimeFormat annotations to handle both JSON deserialization and URL parameter binding

Technical Details:

- @JsonFormat handles JSON request/response body conversion (for PUT/POST operations)
- @DateTimeFormat handles URL parameter conversion (for GET operations with query parameters)
- Both annotations use the "yyyy-MM-dd" pattern for consistency
- The shape = JsonFormat.Shape.STRING ensures proper string-to-Date conversion
- The timezone = "GMT+8" provides proper timezone handling

Result: The project compiles successfully and both functionalities now work correctly:

  • Date range queries via URL parameters (GET /list?returnDateStart=2025-08-20&returnDateEnd=2025-09-09 )
  • Reservation editing via JSON request body (PUT /edit with JSON containing "returnDate": "2025-08-12" )
    Users can now successfully edit reservations without encountering JSON deserialization errors.
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容