Spring 启动 - return 日期(不是时间戳)

Spring Boot - return dates (not as timestamps)

我正在使用 Spring-Boot 1.2.2,代码如下:

@RequestMapping(value = "/dates", method = RequestMethod.GET)
public Date getDates() {
    return new Date();
}

哪个return是这个回复:

1433241315047

我怎样才能做到 return "Sun May 31 16:26:43 IDT 2015" ? 我在 Google 上找到了一些例子,比如 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>

并将 spring.jackson.date-format=yyyy-MM-dd 添加到 application.properties 并且仍然得到时间戳,所以我开始消除所有不必要的代码并发现从我的 WebConfiguration.java 中删除 @Configuration 注释解决了这个问题:

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>>     httpMessageConverters) {

        httpMessageConverters.add(new     MappingJackson2HttpMessageConverter());
    }
}

我想这 class 以某种方式覆盖了日期格式设置...所以我可以在这里指定日期格式吗?

添加依赖项添加对 com.fasterxml.jackson.datatype:jackson-datatype-joda 的依赖项,并将 spring.jackson.serialization.write-dates-as-timestamps: false 添加到您的 application.properties 文件中。

这里有一个类似的 post - json date format in spring-boot

在你的 application.properties 添加 spring.jackson.date-format= # 日期格式字符串(例如 yyyy-MM-dd HH:mm:ss),或完全限定的日期格式 class 名称(例如 com.fasterxml.jackson.databind.util.ISO8601DateFormat)

您可以尝试编写自定义日期反序列化程序 -

//CustomDateSerializer class

public class CustomDateSerializer extends JsonSerializer<Date> {   
}

但是,在这种情况下,您需要注释日期的 getter 方法 @JsonSerialize(using = CustomDateSerializer.class)

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

spring.jackson.date-format=yyyy-MM-ddapplication.properties 作品中