Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Resolving Date Parsing Exceptions in Spring Boot REST Endpoints

Tech 1

Reproducing the Error

Domain Model

java import lombok.Data; import java.util.Date;

@Data public class UserProfile { private String username; private String location; private Date createdAt; }

Endpoint Definition

java @PostMapping("/dates/submit") @ResponseBody public String processDate(UserProfile profile) { return profile.getCreatedAt().toString(); }

Scenario 1: Query Parameter Submission

When the client transmits the date as a URL query string (e.g., ?createdAt=2024-02-09 22:22:33), the server throws a conversion exception.

text ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2024-02-09 22:22:33'; nested exception is java.lang.IllegalArgumentException

Scenario 2: Multipart Form Data Submission

Submitting the value via form-data (e.g., createdAt=2024-02-07) results in the exact same conversion failure.

text ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] to value '2024-02-07'; nested exception is java.lang.IllegalArgumentException

Scenario 3: JSON Payload Submission

To accept JSON, the @RequestBody annotation must be added to the controller method:

java @PostMapping("/dates/submit") @ResponseBody public String processDate(@RequestBody UserProfile profile) { return profile.getCreatedAt().toString(); }

Using a simple date pattern (yyyy-MM-dd) like "2024-06-08" in the JSON body deserializes successfully. However, sending a timestamp with time components (yyyy-MM-dd HH:mm:ss), such as "2024-06-08 22:11:33", triggers a Jackson parsing error.

text HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type java.util.Date from String "2024-06-08 22:11:33": not a valid representation (error: Failed to parse Date value '2024-06-08 22:11:33': Cannot parse date "2024-06-08 22:11:33": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null))

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.