2015-07-25 7 views
8

Ich habe ein Problem mit der JS Serialisierung von ZonedDateTime. Wenn es in json konvertiert wird, erzeugt es ein riesiges Objekt und ich möchte nicht, dass alle Daten jedes Mal übertragen werden. Also habe ich versucht, es nach ISO zu formatieren, aber es funktioniert nicht. Wie kann ich es formatieren?Spring Data JPA - ZonedDateTime-Format für JSON-Serialisierung

Hier ist meine Entity Klasse:

@MappedSuperclass 
public abstract class AuditBase { 

    @Id 
    @GeneratedValue 
    private Long id; 

    @CreatedDate 
    private ZonedDateTime createdDate; 

    @LastModifiedDate 
    private ZonedDateTime lastModifiedDate; 

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) 
    public ZonedDateTime getLastModifiedDate() { 
     return lastModifiedDate; 
    } 

    public void setLastModifiedDate(ZonedDateTime lastModifiedDate) { 
     this.lastModifiedDate = lastModifiedDate; 
    } 

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) 
    public ZonedDateTime getCreatedDate() { 
     return createdDate; 
    } 

    public void setCreatedDate(ZonedDateTime createdDate) { 
     this.createdDate = createdDate; 
    } 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    @PrePersist 
    public void prePersist() { 
     this.createdDate = ZonedDateTime.now(); 
     this.lastModifiedDate = ZonedDateTime.now(); 
    } 

    @PreUpdate 
    public void preUpdate() { 
     this.lastModifiedDate = ZonedDateTime.now(); 
    } 
} 

Antwort

17

Ich denke, dass Sie Jackson für Json Serialisierung verwenden, Jackson hat jetzt ein Modul für Java 8 neue Datum Zeit API, https://github.com/FasterXML/jackson-datatype-jsr310.

diese Abhängigkeit in die pom.xml hinzufügen

<dependency> 
    <groupId>com.fasterxml.jackson.datatype</groupId> 
    <artifactId>jackson-datatype-jsr310</artifactId> 
    <version>2.6.0</version> 
</dependency> 

Und das ist seine Nutzung:

public static void main(String[] args) throws JsonProcessingException { 
    ObjectMapper objectMapper = new ObjectMapper(); 
    objectMapper.registerModule(new JavaTimeModule()); 
    System.out.println(objectMapper.writeValueAsString(new Entity())); 
} 

static class Entity { 
    ZonedDateTime time = ZonedDateTime.now(); 

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ") 
    public ZonedDateTime getTime() { 
     return time; 
    } 
} 

Die Ausgabe lautet:

{"time":"2015-07-25T23:09:01.795+0700"} 

Hinweis: Wenn Sie Ihre Jackson Version 2.4.x Verwendung

objectMapper.registerModule(new JSR310Module()); 

Hoffe, das hilft!

+0

ich etwas sehr Seltsames hatte: {"Zeit": 1473923145.038000000}. Mit Ihrer Lösung habe ich jetzt {"Zeit": "2016-09-15T07: 57: 26.602 + 0000"} wie gewünscht. –

+0

Für mich reichte es mit registerModule. Ich musste die Entität nicht berühren. Vielen Dank!! –

+0

Ich bekomme immer die Schüttelfrost, wenn jemand eine 5 Zeilen XML aus einer Pom-Datei, Gralde für den Sieg;) –

2

Above Antwort funktioniert, aber wenn Sie mit ZonedDateTime nicht möchten, dass Ihre bestehende Entitätsklasse berühren, werden folgende Einstellungen funktionieren:

public static ObjectMapper getMapper() { 
    ObjectMapper mapper = new ObjectMapper(); 
    mapper.registerModule(new JavaTimeModule());  
    return mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false) 
.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS,false) 
         .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false) 
         .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) 
         .setVisibility(mapper.getSerializationConfig() 
           .getDefaultVisibilityChecker() 
           .withFieldVisibility(JsonAutoDetect.Visibility.ANY) 
           .withGetterVisibility(JsonAutoDetect.Visibility.NONE) 
           .withSetterVisibility(JsonAutoDetect.Visibility.NONE) 
           .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); 
    } 

Library:

<dependency> 
      <groupId>com.fasterxml.jackson.dataformat</groupId> 
      <artifactId>jackson-dataformat-csv</artifactId> 
      <version>${jackson.dataformat.csv.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>com.fasterxml.jackson.datatype</groupId> 
      <artifactId>jackson-datatype-jsr310</artifactId> 
     </dependency>