Spring Boot - return dates (not as timestamps)

I am using Spring-Boot 1.2.2 with this code:

@RequestMapping(value = "/dates", method = RequestMethod.GET)
public Date getDates() {
    return new Date();
}

which returns this answer:

1433241315047

How can i get it back "Sun May 31 16:26:43 IDT 2015"? I found several examples on Google, such as mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false), but cannot figure out where I should 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-ddin application.properties and still get timestamps, so I started eliminating all unnecessary codes and found that removing the annotation @Configurationfrom mine WebConfiguration.javasolves this problem:

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 assume that this class somehow overrides the date format setting ... So is it possible to specify a date format here?

+4
source share
2 answers

com.fasterxml.jackson.datatype: jackson-datatype-joda spring.jackson.serialization.write-date-as-timestamps: false application.properties.

- json spring -boot

application.properties spring.jackson.date-format = # (, yyyy-MM-dd HH: mm: ss) (, com.fasterxml.jackson.databind.util.ISO8601DateFormat )

-

// CustomDateSerializer

public class CustomDateSerializer extends JsonSerializer<Date> {   
}

getter @JsonSerialize ( = 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();
}
  }
+7

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

+3

All Articles