【发布时间】:2019-04-09 00:02:12
【问题描述】:
我有一个动态 JSON 模式,我需要在运行时将其转换为 Java 源代码
我发现这个杰克逊的例子看起来很 common
代码运行良好,没有异常但没有生成任何东西。
当我打破 json 结构(只是为了测试 jackson 是否正常工作)时,我确实得到了 Jackson 异常......
@Test
public void jsonToJava() throws IOException {
JCodeModel codeModel = new JCodeModel();
String schemaContents ="{\"test\":\"test\"}";
GenerationConfig config = new DefaultGenerationConfig() {
@Override
public boolean isGenerateBuilders() {
return true;
}
};
SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(config), new SchemaStore()), new SchemaGenerator());
mapper.generate(codeModel, "HelloWorldClass", "com.my.package", schemaContents);
File directory = new File("C:\\temp\\gen");
directory.mkdirs();
codeModel.build(directory);
}
【问题讨论】:
-
如果你投反对票,请解释