简体   繁体   中英

How to format the json date format using spring boot

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", ie, 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; 
}

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

Kindly help me out in resolving this issue.

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM