使用Spring的Joda Time datetime格式无效 [英] Joda Time datetime invalid format with Spring

查看:578
本文介绍了使用Spring的Joda Time datetime格式无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目已经使用Jackson Hibernate4Module进行ObjectMapping。现在,我想在项目中使用Joda Time,并添加了

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

到pom文件。

在我的配置文件中,我有两个转换器初始值设定项,由 configureMessageConverters

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);
}

并修改 DateTime 实体中的字段:

@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]

如何解决此问题?

关于Jackson与多个消息转换器的适用性,没有任何信息。

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

更多信息

给定UTC时间字符串

Sat Nov 01 20:08:07 UTC 2014

此抛出尝试创建 DateTime LocalDateTime 时, IllegalArgumentException 对象。

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

所有这些抛出 IllegalArgumentExcept离子

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")

然而,这有效:

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

更新

无论出于何种原因,似乎JodaTime都不会解析字符串星期六01 01 20:08:07 UTC 2014进入 DateTime LocalDateTime 对象,因为它会一直抛出 IllegalArgumentException

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

日期字符串的来源直接来自JPG的EXIF元数据上传图片。目前,我已经回到使用常规 java.util.Date 对象,因为这些数据仅用于显示和统计信息收集,无需任何操作。

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.

推荐答案

看起来您正在注册两个可以处理相同内容的转换器,因此它们竞争相同的类型。 Spring MVC将迭代所有转换器以查看哪个转换器可以处理内容。

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.

相反,您需要添加两个模块(Joda和Hibernate4)的单个转换器。

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

BTW你可能想看看Jadira项目,它在Hibernate中为Joda usertypes提供了非常优雅的支持: http://jadira.sourceforge.net/usertype-userguide.html

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;

这篇关于使用Spring的Joda Time datetime格式无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆