在我們的項目中,我們使用spring data rest(MongoDB)。 文件:轉換LocalDateTime字段
@Document(collection = "brands")
@Data
@Accessors(chain = true)
public class BrandDocument {
@Id
private String id;
@CreatedDate
private LocalDateTime createdDate;
@LastModifiedDate
private LocalDateTime lastModifiedDate;
private String name;
private Set<String> variants;
}
配置:
@SpringBootApplication
@EnableDiscoveryClient
@EnableMongoAuditing
public class DictionaryService extends RepositoryRestConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(new Object[]{ DictionaryService.class }, args);
}
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.exposeIdsFor(BrandDocument.class);
}
}
如果我試圖讓BrandDocument:
curl -X GET -H "Cache-Control: no-cache" -H "Postman-Token: c12dbb65-e173-7ff3-2187-bdb6adbdebe9" "http://localhost:7090/typeDocuments/"
我會看到答案:
{
...
"lastModifiedDate": {
"content": "2016-08-10T15:50:05.609"
}
...
}
在gradle這個DEPS我有爲轉換ja VA 8 LocalDateTime:
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310'
如果我試圖保存對象,發送POST請求http://localhost:7090/typeDocuments/與內容:
{
...
"lastModifiedDate": {
"content": "2016-08-10T15:50:05.609"
}
...
}
我有轉換錯誤,但如果:
{
...
"lastModifiedDate": "2016-08-10T15:50:05.609"
...
}
保存OK 。
爲什麼jackson爲「lastModifiedDate」添加「content」字段?我該如何改變它?
如果使用你的對象映射它並沒有幫助:( – maksaimer
你調試 –