我想将Jackson lib格式的DateTime joda用于dd-MM-yyyy'T'HH:mm:ss
,但它不起作用。
我在stackoverflow.com
上搜索并找到了问题和答案。我尝试使用很多答案。但它仍然是错误的。
它只能格式化为毫秒。
我的pom.xml
文件:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.jadira.usertype</groupId>
<artifactId>usertype.core</artifactId>
<version>3.2.0.GA</version>
</dependency>
<!-- Joda-time hibernate -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time-hibernate</artifactId>
<version>1.4</version>
</dependency>
在User
域中:
@Column(name="date_of_birth")
@NotEmpty
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime dob;
在配置文件中:
@Bean
public MappingJackson2HttpMessageConverter converter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss");
objectMapper.setDateFormat(dateFormat);
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
// objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.registerModule(new JodaModule());
converter.setObjectMapper(objectMapper);
return converter;
}
答案 0 :(得分:0)
我认为您只是缺少以下方法将其注册到消息转换器:
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(converter());
}