系統(tǒng)在第一次運行時,需要初始化一些數(shù)據(jù)到mongoDB中,初始化數(shù)據(jù)是從源庫導(dǎo)出的json永品,里面有字符不是標準的如"$oid"遭京, "$date"等胃惜,需要轉(zhuǎn)成正常的java對象然后插入庫。
jsonWriterSettings = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED)
.dateTimeConverter(new JsonDateTimeConverter()).objectIdConverter(new ObjectIdConverter()).build();
private <T extends Entity> List<T> initData(String fileName, Class<T> entityClass) {
try (InputStream resourceAsStream = this.getClass().getResourceAsStream("/script/" + fileName)) {
List<T> entities = new ArrayList<>();
for (String readLine : IOUtils.readLines(resourceAsStream, StandardCharsets.UTF_8)) {
Document parse = Document.parse(readLine);
entities.add(JSONUtil.read(parse.toJson(jsonWriterSettings), entityClass));
}
return entities;
} catch (IOException e) {
log.error("初始化報錯", e);
return Collections.emptyList();
}
}