0
Optional
をJava 8
でテストしており、serialize/deserialize
LocalDate
からJson
を入力したいと考えています。だから私は、私はJdk8Module
を登録するカスタムObject Mapper
を作成している:私はそれがスローすべてItem
データを取り、私のコントローラからGET
方法を行う際にLocalDateを逆シリアル化するJdk8Moduleでオプションでエラーが発生する
public class Item implements Serializable{
@JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate insertDate;
public Item(){}
public Item(LocalDate insertDate) {
this.insertDate = insertDate;
}
public Optional<LocalDate> getInsertDate() {
return Optional.ofNullable(insertDate);
}
}
しかし:
@Bean
public ObjectMapper objectMapperBuilder(){
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new Jdk8Module());
return objectMapper;
}
そして、私のEntity
クラスには、次のようになります。
Resolved exception caused by Handler execution:
org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON: java.util.Optional cannot be cast to java.time.LocalDate;
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
java.util.Optional cannot be cast to java.time.LocalDate(through reference chain:
org.springframework.data.domain.PageImpl["content"]
->java.util.Collections$UnmodifiableRandomAccessList[0]
->com.inventory.models.Item["insertDate"])
コードを改善する方法やコードに追加すべき点それを動作させるには?