我正在嘗試讀取一個 json 檔案并轉換為 Object,這對于其他類來說效果很好,但在這種情況下,我的模型GeoJsonMultiPolygon來自package org.springframework.data.mongodb.core.geo;并且不包含沒有引數的建構式
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.data.mongodb.core.geo.GeoJsonMultiPolygon` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (BufferedInputStream); line: 12, column: 5] (through reference chain: java.util.ArrayList[0]->data.countryboundaries.CountryBoundaries["geometry"])
模型:
@Getter
@Setter
@ToString()
@EqualsAndHashCode
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@Document(collection = "country-boundaries")
public class CountryBoundaries {
@Id
private String id;
@EqualsAndHashCode.Exclude
private String type;
@EqualsAndHashCode.Exclude
private CountryBoundariesProperties properties;
@EqualsAndHashCode.Exclude
private GeoJsonMultiPolygon geometry;
}
代碼:
ObjectMapper mapper = new ObjectMapper();
InputStream is = Country.class.getResourceAsStream("/country-boundaries.json");
List<CountryBoundaries> countryBoundaries = mapper.readValue(is, new TypeReference<>(){});
countryBoundariesRepository.saveAll(countryBoundaries);
由于我無法從庫中修改代碼并使用 ObjectMapper 填充幾何物件,如何繞過此錯誤?
uj5u.com熱心網友回復:
檢查GeoJsonModule的靜態方法。它包含包含 geo json 類的序列化器和反序列化器的模塊。
注冊您的ObjectMapper.
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(GeoJsonModule.geoJsonModule());
geoJsonModule()包含序列化器和反序列化器,如果您只需要反序列化器,請使用deserializers().
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/459164.html
上一篇:全域彈簧測驗模擬靜態方法
