java - Spring Boot - return dates (not as timestamps) -


i'm using spring-boot 1.2.2 code:

@requestmapping(value = "/dates", method = requestmethod.get) public date getdates() {     return new date(); } 

which returns response:

1433241315047 

how can make return "sun may 31 16:26:43 idt 2015" ? found examples on google mapper.configure(serializationconfig.feature.write_dates_as_timestamps, false) can't figure out should write this...

update:
added 2 dependencies 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 application.properties , still getting timestamps, started eliminate unnecessary code , found removing @configuration annotation webconfiguration.java solve 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 class somehow override date-format setting... can specify date-format here?

solution:
paul suggested, added webconfiguration class:

    @bean public mappingjackson2httpmessageconverter mappingjackson2httpmessageconverter() {     mappingjackson2httpmessageconverter jsonconverter = new mappingjackson2httpmessageconverter();     objectmapper objectmapper = new objectmapper();     objectmapper.configure(serializationfeature.write_dates_as_timestamps, false);     jsonconverter.setobjectmapper(objectmapper);     return jsonconverter; } 

add dependency add dependency on com.fasterxml.jackson.datatype:jackson-datatype-joda , add spring.jackson.serialization.write-dates-as-timestamps: false application.properties file.

here similar post - json date format in spring-boot

in application.properties add spring.jackson.date-format= # date format string (e.g. yyyy-mm-dd hh:mm:ss), or fully-qualified date format class name (e.g. com.fasterxml.jackson.databind.util.iso8601dateformat)

you try writing custom date deserializer -

//customdateserializer class

public class customdateserializer extends jsonserializer<date> {    } 

however, in case need annotate 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(); }   } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

node.js - Using Node without global install -

php - CakePHP HttpSockets send array of paramms -