2017-02-15 73 views
0

我有一個沒有Spring Boot的項目,但它使用了一些彈簧模塊,比如「spring data」和「spring data rest」。Spring Data Rest和java.time序列化

我對java.time。*字段的序列化有一些問題。 我發現一些教程就像this但即使我加入我的RepositoryRestConfigurerAdapter

@Component public class CvlRepositoryRestConfigurerAdapter extends RepositoryRestConfigurerAdapter { 

@Override 
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { 
    config.setDefaultPageSize(75); 
    config.setReturnBodyForPutAndPost(Boolean.TRUE); 
} 

@Override 
public void configureJacksonObjectMapper(ObjectMapper objectMapper) { 
    super.configureJacksonObjectMapper(objectMapper); 
    objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false); 
} 

}

以下依賴

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

和下面的代碼我的實際響應關於Java的。時間字段如下

「rateDate」:{ 「年」:2017年, 「月」: 「二月」, 「請將dayOfMonth」:14, 「一週中的某天」: 「星期二」, 「時代」: 「CE」, 「DAYOFYEAR」:45, 「leapYear」 :假的, 「monthValue」:2, 「年表」:{ 「ID」: 「ISO」, 「calendarType」: 「ISO8601」 }

我做錯了嗎?我忘了什麼?

回答

0

這裏是我的適配器。現在,它的工作

@Component 
public class CvlRepositoryRestConfigurerAdapter extends RepositoryRestConfigurerAdapter { 

@Override 
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { 
    config.setDefaultPageSize(75); 
    config.setReturnBodyForPutAndPost(Boolean.TRUE); 
} 

@Override 
public void configureJacksonObjectMapper(ObjectMapper objectMapper) { 
    super.configureJacksonObjectMapper(objectMapper); 
    objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false); 
    SimpleModule sm = new SimpleModule("jsr310module"); 
    sm.addSerializer(LocalDate.class,new LocalDateSerializer(DateTimeFormatter.ISO_DATE)); 
    sm.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ISO_DATE)); 
    sm.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateTimeFormatter.ISO_DATE_TIME)); 
    sm.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME)); 
    objectMapper.registerModule(sm); 
} 

}

我只需要檢查的時區(ZonedDateTime場)和編碼(似乎是UTF-8是默認值),都將被罰款。 希望對他人有用

相關問題