本文介绍了如何使用spring boot格式化json日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring boot和gradle来创建休息服务。现在我需要以yyyy-MM-dd的形式格式化json日期,即格式应该是dateOfBirth:16-03-2015,但我得到的是dateOfBirth:-751181400000。我在我的Apllication.java类中添加了以下代码,但仍然无法获得所需的输出。

I am working on spring boot and gradle for creating a rest service. Now I need to format the json date in the form of "yyyy-MM-dd", i.e, the format should be dateOfBirth: "16-03-2015", but I am getting "dateOfBirth: -751181400000". I added the below piece of code in my Apllication.java class, but still not able to get the desired output.

@Bean
@ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class })
public Jackson2ObjectMapperBuilder jacksonBuilder()
{
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.indentOutput(true).dateFormat(new SimpleDateFormat("yyyy-MM-dd"));
    return builder;
}

和Application.java:

And Application.java:

@Configuration
@Import(SubjectServiceConfig.class)
@EnableAutoConfiguration
@EnableEurekaClient
@ComponentScan({"com.billing"})
@EnableWebMvc
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class Application {
public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}
}

请帮我解决这个问题。

推荐答案

使用Spring Boot,您应该可以通过在application.yml中设置以下属性来设置Jackson格式化日期的默认方式/application.properties:

With Spring Boot, you should be able to set the default way that Jackson formats dates by setting the following property in your application.yml/application.properties:

spring.jackson.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.jackson.databind.util.ISO8601DateFormat)

这篇关于如何使用spring boot格式化json日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:24