java - Changing default json for an object in Spring Boot Application -


  • i have simple spring boot application having simple mydatetime model class having java.util.date instance variable private access, getters/setters , default constructor.

  • a controller instantiates object , returns back.

  • in output, see default representation of date object done integer (maybe millis epoch)

  • is there way can change default jsonification of date object iso-string or other string?

edit:

some clarification:

i'm new spring , spring boot. i'm using template sample application on spring's website. jsonification done through jackson. rest, don't know spring in general.

you can set default format jackson uses when serializing dates in application.properties file:

spring.jackson.date-format=yyyy-mm-dd hh:mm:ss 

http://docs.spring.io/spring-boot/docs/1.2.3.release/reference/htmlsingle/#common-application-properties

you can specify specific format use specific date using @jsonformat annotation, example below:

example pojo:

public class demo {     private date timestamp1;     private date timestamp2;     public date gettimestamp1() {         return timestamp1;     }     public void settimestamp1(date timestamp1) {         this.timestamp1 = timestamp1;     }     @jsonformat(shape=jsonformat.shape.string, pattern="yyyy-mm-dd't'hh:mm:ss.sssz")     public date gettimestamp2() {         return timestamp2;     }     public void settimestamp2(date timestamp2) {         this.timestamp2 = timestamp2;     } } 

example controller:

@restcontroller public class democontroller {      @requestmapping(value="/demo", method = requestmethod.get)     demo start() {         demo demo = new demo();         date timestamp = new date();         demo.settimestamp1(timestamp);         demo.settimestamp2(timestamp);         return demo;      } } 

https://github.com/fasterxml/jackson-annotations/wiki/jackson-annotations


Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

c# - SharpSsh Command Execution -

python - Specify path of savefig with pylab or matplotlib -