中是否有任何接口Jackson
,或者为多个对象映射器创建相同配置的更好方法?例如,我对同一个类有这两种方法:
public static File toCsv(List<Entity> entityList) {
final File tempFile = new File(CSV_FILE_NAME);
tempFile.deleteOnExit();
CsvMapper mapper = new CsvMapper();
mapper.findAndRegisterModules()
.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true)
.addMixIn(Entity.class, EntityMixIn.class)
.addMixIn(EntityB.class, EntityBMixIn.class);
CsvSchema schema = mapper.schemaFor(Entity.class).withHeader();
try {
mapper.writer(schema).writeValue(tempFile,entityList);
return tempFile;
} catch (IOException ex) {
throw new Exception();
}
}
}
public static File toXml(List<Entity> entityList) {
final File tempFile = new File(XML_FILE_NAME);
tempFile.deleteOnExit();
XmlMapper mapper = new XmlMapper();
mapper.findAndRegisterModules()
.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true)
.addMixIn(Entity.class, EntityMixIn.class)
.addMixIn(EntityB.class, EntityBMixIn.class);
try {
mapper.writeValue(tempFile, entityList);
return tempFile;
} catch (IOException ex) {
throw new Exception();
}
}
}
并且两者具有完全相同的配置,如果将来我需要一种新格式,我可能会使用相同的配置。