Java 春季启动-返回日期(不作为时间戳)
我将Spring Boot 1.2.2与以下代码一起使用:Java 春季启动-返回日期(不作为时间戳),java,spring,spring-boot,Java,Spring,Spring Boot,我将Spring Boot 1.2.2与以下代码一起使用: @RequestMapping(value = "/dates", method = RequestMethod.GET) public Date getDates() { return new Date(); } 返回以下响应: 1433241315047 如何使其返回“Sun May 31 16:26:43 IDT 2015”? 我在谷歌上找到了一些例子,比如mapper.configure(SerializationC
@RequestMapping(value = "/dates", method = RequestMethod.GET)
public Date getDates() {
return new Date();
}
返回以下响应:
1433241315047
如何使其返回“Sun May 31 16:26:43 IDT 2015”
?
我在谷歌上找到了一些例子,比如mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,false)
,但我不知道应该在哪里写这个
更新:我在pom.xml中添加了2个依赖项:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.4.0</version>
</dependency>
我猜这个类会覆盖日期格式设置。。。那么我可以在这里指定日期格式吗?添加依赖项在com.fasterxml.jackson.datatype:jackson数据类型joda上添加依赖项,并将spring.jackson.serialization.write-dates-as-timestamps:false添加到application.properties文件中 这里有一个类似的帖子- 在application.properties中添加 spring.jackson.date format=#日期格式字符串(例如yyyy-MM-dd HH:MM:ss)或完全限定的日期格式类名(例如com.fasterxml.jackson.databind.util.ISO8601DateFormat) 您可以尝试编写自定义日期反序列化程序- //CustomDateSerializer类
public class CustomDateSerializer extends JsonSerializer<Date> {
}
公共类CustomDateSerializer扩展JsonSerializer{
}
但是,在本例中,您需要注释date@JsonSerialize的getter方法(使用=CustomDateSerializer.class)
@配置
公共类WebConfig扩展了WebMvcConfigurationSupport{
@豆子
公共映射Jackson2HttpMessageConverter自定义Jackson2HttpMessageConverter(){
MappingJackson2HttpMessageConverter jsonConverter=新的MappingJackson2HttpMessageConverter();
ObjectMapper ObjectMapper=新的ObjectMapper();
objectMapper.configure(在未知属性上反序列化feature.FAIL,false);
setObjectMapper(objectMapper);
返回jsonConverter;
}
@凌驾
公共无效配置MessageConverters(在application.properties
works中列出spring.jackson.date format=yyyyy-MM-dd
,使用@EricMartinez我不能…稍后我需要将此应用于使用date
的第三方组件Hi Shoham,请您从问题中删除解决方案并将其添加到答案部分。我添加了com.fasterxml.jackson.datatype jackson datatype joda
到我的pom,并到spring.jackson.serialization.write-dates-as-timestamps=false
到我的application.properties,但它仍然返回时间戳因此尝试添加@JsonFormat(pattern=“yyyy-MM-dd”)
在@RequestMapping之后,但它是same@shoham您可以尝试在application.properties中添加spring.jackson.date format=#日期格式字符串(例如yyyy-MM-dd HH:MM:ss)或完全限定的日期格式类名(例如com.fasterxml.jackson.databind.util.ISO8601DateFormat)我在application.properties中添加了spring.jackson.date format=yyyy-MM-dd
,但没有luck@shoham我所做的所有研究都表明这是可行的。我们可以尝试另一种方法。试着编写一个自定义的日期反序列化程序-
public class CustomDateSerializer extends JsonSerializer<Date> {
}
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Bean
public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(customJackson2HttpMessageConverter());
super.addDefaultHttpMessageConverters();
}
}