Joda Time datetime invalid format with Spring

泄露秘密 提交于 2020-01-04 11:43:23

问题


I have a project that already uses the Jackson Hibernate4Module for ObjectMapping. Now, I want to use Joda Time with the project, and have added

joda-time
joda-time-hibernate
jackson-datatype-joda

to the pom file.

In my config file, I have the two converter initializers which are called by configureMessageConverters

@Bean
public MappingJackson2HttpMessageConverter jacksonMessageConverter(){
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new Hibernate4Module());
    converter.setObjectMapper(mapper);

    return converter;
}

@Bean
public MappingJackson2HttpMessageConverter jodaMessageConverter(){
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JodaModule());
    mapper.setDateFormat(new ISO8601DateFormat());
    converter.setObjectMapper(mapper);
    return converter;
}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converterList){
    converterList.add(jacksonMessageConverter());
    converterList.add(jodaMessageConverter());
    super.configureMessageConverters(converterList);
}

and modified the DateTime fields in the entity:

@Column(name = "upload_date", nullable = false)
@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
@DateTimeFormat(pattern="dd/MM/yy hh:mm:ss")
private DateTime uploadDate;

@Column(name = "capture_date", nullable = false)
@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
@DateTimeFormat(pattern="dd/MM/yy hh:mm:ss")
private DateTime captureDate;

But for unknown reasons, the data is not persisting to the database. I can see the the domain model object being created with the appropriate date values being set. However, it is not replicating to the database and I get an error

21:15:14.892 [http-bio-8080-exec-3] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - 
Written [[Error uploading file 11-1_mbb0067.jpg
Invalid format: "Sat Nov 01 19:34:51 UTC 2014"]] as "application/json;charset=UTF-8" using     
[org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@196c2ab2]

How do I resolve this issue?

There is zero information about the suitability of multiple message converters with Jackson.

More Information

Given a UTC time string

Sat Nov 01 20:08:07 UTC 2014

this throws an IllegalArgumentException when attempting to create both a DateTime and LocalDateTime object.

All these throw IllegalArgumentExceptions:

LocalDateTime ldt = new LocalDateTime("Sat Nov 01 20:08:07 UTC 2014");
LocalDateTime ldt = LocalDateTime.parse("Sat Nov 01 20:08:07 UTC 2014")

DateTime dt = new DateTime("Sat Nov 01 20:08:07 UTC 2014")

However, this works:

Date d = new Date("Sat Nov 01 20:08:07 UTC 2014")

Update

For whatever reason, it seems that JodaTime will not parse the string "Sat Nov 01 20:08:07 UTC 2014" into a DateTime or LocalDateTime object, as it will consistently throw an IllegalArgumentException

The source of the date string is straight from the EXIF metadata of JPG upload images. Currently, I've gone back to using regular java.util.Date objects, since the data is just intended for display and statistics gathering, with no manipulation required.


回答1:


Looks like you're registering two converters which both can handle the same content, so they are competing for the same types. Spring MVC will iterate over all converters to see which one can handle the content.

Instead you want a single converter with both modules (Joda and Hibernate4) added.

BTW you might want to take a look at the Jadira project, which provides very elegant support in Hibernate for Joda usertypes: http://jadira.sourceforge.net/usertype-userguide.html

@Column
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private LocalDateTime dateTime;


来源:https://stackoverflow.com/questions/27136877/joda-time-datetime-invalid-format-with-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!