Spring Boot项目出现LocalDateTime与Timestamp转换异常求助
IllegalArgumentException: Can not set java.time.LocalDateTime field to java.sql.Timestamp After Removing jackson-datatype-jsr310 Hey there, let’s work through this type conversion issue you’re hitting after removing that redundant jackson-datatype-jsr310 dependency. Here’s what’s happening and how to fix it:
Why This Happens
Even though jackson-datatype-jsr310 is mainly for JSON serialization/deserialization of Java 8 time types, removing it can sometimes disrupt the implicit type mapping between java.time.LocalDateTime and java.sql.Timestamp that Spring Boot and Hibernate rely on. Hibernate needs a way to convert between database TIMESTAMP values and your entity’s LocalDateTime fields, and without the right converters (or if they’re not being registered properly), you get that error.
Step-by-Step Solutions
1. Verify Entity Class Annotations
First, make sure your entity’s LocalDateTime fields are mapped correctly to the database. For H2, the column type should be TIMESTAMP, and you don’t need extra @Type annotations if you’re using Hibernate 5.2+ (which is included in modern Spring Boot starters). Example:
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import java.time.LocalDateTime; @Entity public class Task { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(columnDefinition = "TIMESTAMP") // Explicitly define column type for clarity private LocalDateTime createdAt; // Getters, setters, and other fields }
2. Ensure Spring Boot’s Default Time Type Support is Enabled
Spring Boot 2.x and above automatically includes support for Java 8 time types via the spring-boot-starter-web and spring-boot-starter-data-jpa starters. Double-check your pom.xml (or build.gradle) to make sure these starters are present—they handle the core conversion logic between LocalDateTime and Timestamp without needing jackson-datatype-jsr310.
3. Explicitly Register the JavaTimeModule (If Needed)
If you’ve customized your ObjectMapper somewhere in your code, you might have overridden Spring Boot’s default configuration that registers the JavaTimeModule. Add this configuration to ensure the module is registered:
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class JacksonConfig { @Bean public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JavaTimeModule()); return mapper; } }
This ensures both JSON serialization/deserialization and Hibernate’s type conversion work correctly for Java 8 time types.
4. Check for Hibernate Version Compatibility
If you’re using an older version of Hibernate (pre-5.2), you’ll need to add the hibernate-java8 dependency to get support for Java 8 time types. Add this to your pom.xml:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-java8</artifactId> </dependency>
Modern Spring Boot starters include this automatically, so this is only necessary if you’re overriding the Hibernate version manually.
Final Checks
After applying these steps, rebuild your project and test the REST endpoints. The error should go away once Hibernate can properly convert between Timestamp and LocalDateTime again.
内容的提问来源于stack exchange,提问作者Alfred Moon

