【发布时间】:2014-07-07 12:02:27
【问题描述】:
如何在 joda、jackson、hibernate 中设置日期时间格式。
import org.joda.time.DateTime;
import com.fasterxml.jackson.annotation.JsonFormat;
//other imports...
@Entity
public class Example {
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@JsonFormat(pattern = "dd/MM/yyyy'T'HH:mm")
private DateTime eventDateTime;
//getters setters
}
//registration joda jackson module:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JodaModule());
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
DateFormat df = new SimpleDateFormat("dd/MM/yyyy'T'HH:mm");
objectMapper.setDateFormat(df);
//If I send JSON like:
{"eventDateTime": "19/05/2014T14:8"}
//my spring controller:
@RequestMapping(method = RequestMethod.POST)
public void saveExample(@RequestBody Example example ) { ..
我的 Spring 控制器在反序列化 eventDateTime 时总是失败,所以我收到响应错误 400“客户端发送的请求在语法上不正确”。
如果我将日期时间格式更改为:
{"eventDateTime": "2014-05-19T14:8"}
它有效。
【问题讨论】:
标签: spring hibernate spring-mvc jackson jodatime