6

I'm using Spring-Boot 1.2.2 with this code:

@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...

UPDATE:
I added 2 dependencies to pom.xml:

<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>

and added spring.jackson.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:

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

I guess this class somehow override date-format setting... So can I specify the date-format here?

Shoham
  • 7,014
  • 8
  • 40
  • 40

2 Answers2

8

Add the dependency Add a dependency on com.fasterxml.jackson.datatype:jackson-datatype-joda and add spring.jackson.serialization.write-dates-as-timestamps: false to your application.properties file.

Here is a similar post - json date format in spring-boot

In your application.properties add 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)

You could try writing a custom date deserializer -

//CustomDateSerializer class

public class CustomDateSerializer extends JsonSerializer<Date> {   
}

However, in this case you would need to annotate the getter method of date @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();
}
  }
Community
  • 1
  • 1
Paul John
  • 1,626
  • 1
  • 13
  • 15
  • 1
    I added ` com.fasterxml.jackson.datatype jackson-datatype-joda ` to my pom and `spring.jackson.serialization.write-dates-as-timestamps = false` to my application.properties but its still returns timestamps – Shoham Jun 02 '15 at 10:34
  • also tried to add `@JsonFormat(pattern="yyyy-MM-dd")` after @RequestMapping but its the same – Shoham Jun 02 '15 at 10:50
  • @shoham can you try In your application.properties add 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) – Paul John Jun 02 '15 at 11:07
  • 2
    I added `spring.jackson.date-format=yyyy-MM-dd` to application.properties but no luck – Shoham Jun 02 '15 at 11:24
  • 1
    @shoham all the research i did points that this shuld work. There is another method we could try out..try writing a custom date deserializer - http://stackoverflow.com/questions/9038005/spring-3-1-json-date-format – Paul John Jun 02 '15 at 11:47
  • I would use `Jackson2ObjectMapperBuilderCustomizer` instead – herman Apr 10 '17 at 10:11
5

spring.jackson.date-format=yyyy-MM-dd in application.properties works

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
GX9457
  • 51
  • 1
  • 1