无法从POJO生成Json模式

时间:2018-03-26 10:12:44

标签: java json jackson jsonschema

我使用单个POJO并且工作正常,我决定使用org.apache.commons中的FileUtils来处理一堆java类,以便遍历所有java文件目录并在generateSchema()中使用get class()杰克逊模块,但输出不是预期的。

我不知道问题出在哪里?

输入:

File dir = new File("./src/main/java/com/hiveprod/generator/jsonshema");
        String[] extensions = new String[] { "java" };
        System.out.println("Getting POJOs..." );
        List<File> files = (List<File>) FileUtils.listFiles(dir, extensions, true);
            for (File file : files) {
            String className=file.getName();
            //Mapping...
             ObjectMapper mapper = new ObjectMapper();
            JsonSchemaGenerator generator = new JsonSchemaGenerator(mapper);

            JsonSchema jsonSchema = generator.generateSchema(file.getClass());
            StringWriter jsonSchemafile = new StringWriter();
            mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
            mapper.writeValue(jsonSchemafile, jsonSchema);
  
    

一个文件的示例

  

输出:

{
  "type" : "string"
}

预期产出:

{
  "type" : "object",
  "properties" : {
    "result" : {
      "type" : "string"
    },
    "subtitle1" : {
      "type" : "string"
    },
    "infoSup" : {
      "type" : "string"
    },
    "subtitle" : {
      "type" : "string"
    },
    "title" : {
      "type" : "string"
    },
    "info" : {
      "type" : "string"
    }
  }
}

2 个答案:

答案 0 :(得分:0)

generator.generateSchema(file.getClass());

文件是一个文件而不是您想要的类。我认为你至少需要Class,甚至可以实例化它:

Class c = Class.forName(className);
generator.generateSchema(c.getClass());

如果你必须实例化它,每个类都应该有一个空的默认构造函数,而Reflection应该得到这个:

Constructor ctor = c.getConstructor(); // use c again
Object instanceOfTheClass = ctor.newInstance();
generator.generateSchema(instanceOfTheClass.getClass());

答案 1 :(得分:0)

下面的代码安静有助于我获取给定包中的所有类

 private static Class[] getClasses(String packageName)
            throws ClassNotFoundException, IOException {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        assert classLoader != null;
        String path = packageName.replace('.', '/');
        Enumeration resources = classLoader.getResources(path);
        List dirs = new ArrayList();
        while (resources.hasMoreElements()) {
            URL resource = resources.nextElement();
            dirs.add(new File(resource.getFile()));
        }
        ArrayList classes = new ArrayList();
        for (File directory : dirs) {
            classes.addAll(findClasses(directory, packageName));
        }
        return classes.toArray(new Class[classes.size()]);
    }

      private static Class[] getClasses(String packageName)
            throws ClassNotFoundException, IOException {
        ClassLoader classLoader =Thread.currentThread()
       .getContextClassLoader();
        assert classLoader != null;
        String path = packageName.replace('.', '/');
        Enumeration resources = classLoader.getResources(path);
        List dirs = new ArrayList();
        while (resources.hasMoreElements()) {
            URL resource = resources.nextElement();
            dirs.add(new File(resource.getFile()));
        }
        ArrayList classes = new ArrayList();
        for (File directory : dirs) {
            classes.addAll(findClasses(directory, packageName));
        }
        return classes.toArray(new Class[classes.size()]);
    }

了解详情:https://dzone.com/articles/get-all-classes-within-package