java Spring Boot - 返回日期(不是时间戳)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30558047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot - return dates (not as timestamps)
提问by Shoham
I'm using Spring-Boot 1.2.2 with this code:
我正在使用带有以下代码的 Spring-Boot 1.2.2:
@RequestMapping(value = "/dates", method = RequestMethod.GET)
public Date getDates() {
return new Date();
}
which returns this response:
返回此响应:
1433241315047
How can I make it return "Sun May 31 16:26:43 IDT 2015"
?
I found some examples on Google like mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false)
but can't figure out where should I write this...
我怎样才能让它回来"Sun May 31 16:26:43 IDT 2015"
?我在谷歌上找到了一些例子,mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false)
但不知道我应该在哪里写这个......
UPDATE:
I added 2 dependencies to pom.xml:
更新:
我向 pom.xml 添加了 2 个依赖项:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.datatype</groupId>
<artifactId>Hymanson-datatype-joda</artifactId>
<version>2.4.0</version>
</dependency>
and added spring.Hymanson.date-format=yyyy-MM-dd
to application.properties
and still getting timestamps, so I started to eliminate all unnecessary code and found that removing the @Configuration
annotation from my WebConfiguration.java
solve the this issue:
并添加spring.Hymanson.date-format=yyyy-MM-dd
到application.properties
并仍然得到时间戳,所以我开始消除所有不必要的代码,发现@Configuration
从我的注释中删除可以WebConfiguration.java
解决这个问题:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter;
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 MappingHymanson2HttpMessageConverter());
}
}
I guess this class somehow override date-format setting... So can I specify the date-format here?
我猜这个类以某种方式覆盖了日期格式设置......那么我可以在这里指定日期格式吗?
回答by Paul John
Add the dependency Add a dependency on com.fasterxml.Hymanson.datatype:Hymanson-datatype-joda and add spring.Hymanson.serialization.write-dates-as-timestamps: false to your application.properties file.
添加依赖项 添加对 com.fasterxml.Hymanson.datatype:Hymanson-datatype-joda 的依赖项,并将 spring.Hymanson.serialization.write-dates-as-timestamps: false 添加到您的 application.properties 文件中。
Here is a similar post - json date format in spring-boot
这是spring-boot中类似的post- json日期格式
In your application.properties add spring.Hymanson.date-format= # Date format string (e.g. yyyy-MM-dd HH:mm:ss), or a fully-qualified date format class name (e.g. com.fasterxml.Hymanson.databind.util.ISO8601DateFormat)
在您的 application.properties 中添加 spring.Hymanson.date-format= # 日期格式字符串(例如 yyyy-MM-dd HH:mm:ss),或完全限定的日期格式类名称(例如 com.fasterxml.Hymanson.databind .util.ISO8601DateFormat)
You could try writing a custom date deserializer -
您可以尝试编写自定义日期解串器 -
//CustomDateSerializer class
//CustomDateSerializer 类
public class CustomDateSerializer extends JsonSerializer<Date> {
}
However, in this case you would need to annotate the getter method of date @JsonSerialize(using = CustomDateSerializer.class)
但是,在这种情况下,您需要注释日期 @JsonSerialize(using = CustomDateSerializer.class) 的 getter 方法
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Bean
public MappingHymanson2HttpMessageConverter customHymanson2HttpMessageConverter() {
MappingHymanson2HttpMessageConverter jsonConverter = new MappingHymanson2HttpMessageConverter();
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(customHymanson2HttpMessageConverter());
super.addDefaultHttpMessageConverters();
}
}
回答by GX9457
spring.Hymanson.date-format=yyyy-MM-dd
in application.properties
works
spring.Hymanson.date-format=yyyy-MM-dd
在application.properties
作品中