我使用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"
?
我在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
并且仍然获得时间戳,所以我开始消除所有不必要的代码,发现从@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.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());
}
}
我想这个类会以某种方式覆盖日期格式设置......那么我可以在这里指定日期格式吗?
答案 0 :(得分:8)
添加依赖项在com.fasterxml.jackson.datatype:jackson-datatype-joda上添加依赖项,并将spring.jackson.serialization.write-dates-as-timestamps:false添加到application.properties文件中。
以下是类似的帖子 - json date format in spring-boot
在你的application.properties中添加 spring.jackson.date-format =#日期格式字符串(例如yyyy-MM-dd HH:mm:ss),或完全限定的日期格式类名称(例如com.fasterxml.jackson.databind.util.ISO8601DateFormat)< / p>
您可以尝试编写自定义日期反序列化程序 -
// CustomDateSerializer类
public class CustomDateSerializer extends JsonSerializer<Date> {
}
但是,在这种情况下,您需要注释date @JsonSerialize的getter方法(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();
}
}
答案 1 :(得分:4)
if ('serviceWorker' in navigator && 'PushManager' in window) {
console.log('Service Worker and Push is supported');
navigator.serviceWorker.register('service-worker.js')
.then(function(swReg) {
console.log('Service worker successfully registered.');
navigator.serviceWorker.ready.then(function() {
const subscribeOptions = {
userVisibleOnly: true,
applicationServerKey: ... // My application server key.
};
let promise = swReg.pushManager.subscribe(subscribeOptions)
console.log(promise) // Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
promise
.then(function(pushSubscription) {
console.log('Received PushSubscription: ', pushSubscription); // Never gets called.
})
.catch(function(err) {
console.error('Unable to subscribe.', err); // Never gets called.
});
})
})
.catch(function(err) {
console.error('Unable to register service worker.', err);
});
}
中的 spring.jackson.date-format=yyyy-MM-dd
无效