Spring Boot 1.5.6集成Jackson 2.8.8时反序列化String到Joda-Time失败
Hey there, I see you've already added the jackson-datatype-joda dependency (great first step!), but the error is still happening because Jackson isn't properly leveraging the Joda module to convert that string timestamp into a Joda DateTime object. Let's go through a few reliable fixes:
Solution 1: Explicitly Enable Joda Module in Spring Boot's Jackson Configuration
Spring Boot 1.5.x should auto-detect and register Jackson modules from the classpath, but sometimes custom ObjectMapper setups or configuration conflicts can break this. You can force the module to load by defining a Jackson2ObjectMapperBuilder bean:
@Configuration public class JacksonConfig { @Bean public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); // Install the Joda module to handle DateTime serialization/deserialization builder.modulesToInstall(new JodaModule()); // Optional: Match the date format returned by your REST API builder.simpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); return builder; } }
Solution 2: Manually Register JodaModule with Your ObjectMapper
If you've created a custom ObjectMapper bean in your app, you need to explicitly register the Joda module with it:
@Bean public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new JodaModule()); // Disable timestamp-based date handling (use string format instead) objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); return objectMapper; }
Solution 3: Annotate Individual DateTime Fields
If only specific fields are causing issues, you can directly specify the Joda deserializer on the field itself:
import com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer; import org.joda.time.DateTime; public class YourApiResponse { @JsonDeserialize(using = DateTimeDeserializer.class) private DateTime timestamp; // Getters and setters }
Bonus: Configure Global Date Handling in application.properties
Add this line to your application.properties to ensure Jackson uses string-based date formatting by default (instead of timestamps), which aligns with your API's response format:
spring.jackson.serialization.write-dates-as-timestamps=false
Any of these approaches should fix the error—they all tell Jackson how to properly convert that string timestamp into a Joda DateTime instance, instead of trying (and failing) to use a non-existent String constructor.
内容的提问来源于stack exchange,提问作者Tom

