【问题标题】:Generating JsonSchema from pojo: how do I add "description" automatically?从 pojo 生成 JsonSchema:如何自动添加“描述”?
【发布时间】:2014-08-22 08:19:45
【问题描述】:

我正在尝试在我的项目中从 pojos 自动生成 JsonSchema:代码如下所示:

ObjectMapper mapper = new ObjectMapper();
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
mapper.acceptJsonFormatVisitor(clazz, visitor);
JsonSchema jsonSchema = visitor.finalSchema();
String schemaString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);

当 clazz 被这样定义时:

public class ZKBean
{
  public String anExample;
  public int anInt;
}

I end up with this:

{
  "type" : "object",
  "id" : "urn:jsonschema:com:emc:dpad:util:ZKBean",
  "properties" : {
    "anInt" : {
      "type" : "integer"
    },
    "anExample" : {
      "type" : "string"
    }
  }
}

这一切都很棒。我想要做的是将“描述”键添加到架构中,这样我就有了如下所示的内容:

{
  "type" : "object",
  "id" : "urn:jsonschema:com:emc:dpad:util:ZKBean",
  "properties" : {
    "anInt" : {
      "type" : "integer",
      "description" : "Represents the number of foos in the system"
    },
    "anExample" : {
      "type" : "string",
      "description" : "Some descriptive description goes here"
    }
  }
}

我以为我可以在我的 ZKBean 类中的字段上添加一些注释,但是经过半天的尝试,我还没有找到。这是要走的路吗?还是我需要对我的访客做点什么?

谢谢, 杰西

【问题讨论】:

    标签: java json jackson jsonschema


    【解决方案1】:

    您可以使用 @JsonPropertyDescription 注释来生成自 Jackson 2.4.1 起有效的 json 模式。这是一个例子:

    public class JacksonSchema {
        public static class ZKBean {
            @JsonPropertyDescription("This is a property description")
            public String anExample;
            public int anInt;
        }
    
        public static void main(String[] args) throws JsonProcessingException {
            ObjectMapper mapper = new ObjectMapper();
            SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
            mapper.acceptJsonFormatVisitor(ZKBean.class, visitor);
            JsonSchema jsonSchema = visitor.finalSchema();
            System.out.println(mapper
                    .writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema));
        }
    }
    

    输出:

    {
      "type" : "object",
      "id" : "urn:jsonschema:stackoverflow:JacksonSchema:ZKBean",
      "properties" : {
        "anExample" : {
          "type" : "string",
          "description" : "This is a property description"
        },
        "anInt" : {
          "type" : "integer"
        }
      }
    }
    

    【讨论】:

    • 感谢您的回答,但由于某种原因,它对我不起作用;注释只是被忽略了。您是否能够使其工作,并且 ZKBean 作为静态内部类重要吗?我发现了这个:github.com/FasterXML/jackson-annotations/pull/13 这似乎表明 jackson-module-jsonSchema 可能仍然存在错误。编辑:尝试将其作为内部静态类并没有解决问题。
    • 似乎支持@JsonPropertyDescription 的功能尚未添加到jackson-module-jsonSchema 项目中。 github.com/FasterXML/jackson-module-jsonSchema/issues/37
    • 我正在使用 2.4.1 的所有内容(jackson-annotations、jackson-core、jackson-databind、jackson-module-jsonSchema),但我看到的行为仍然是 @JsonPropertyDescription 被忽略。还有什么我可以尝试让它工作的吗?
    • 嗯,我创建了一个示例项目 github.com/Alexey1Gavrilov/stackoverflow 。它对你有用吗?
    • 是的,这对我有用;问题是我的类文件缺少注释。
    【解决方案2】:

    看来问题不在于杰克逊库,而在于我用来生成类对象的包。我的代码粘贴在下面;我怀疑 Reflections 对象正在删除注释信息(当我手动指定 ZKBean.class 时,会出现描述)。感谢您的帮助!

    Reflections reflections = new Reflections(
            new ConfigurationBuilder().setUrls(
                ClasspathHelper.forClassLoader(urlcl)).
                addClassLoader(urlcl));
        Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(JsonBean.class);
        ObjectMapper mapper = new ObjectMapper();
        SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
        for (Class<?> clazz : annotated)
        {
          try
          {
            mapper.acceptJsonFormatVisitor(mapper.constructType(SampleBean.class), visitor);
            JsonSchema jsonSchema = visitor.finalSchema();
            String schemaString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
            System.out.println(schemaString);
    

    【讨论】:

    • 更正:Reflections 包可能不是罪魁祸首。由于某种原因,我的 jar 文件中的类没有保留它们的注释。
    • 很高兴你终于解决了这个问题!我想请你接受我的回答,这样其他人就会知道属性描述实际上是从 2.4.1 开始工作的。谢谢!
    • @Jessy 请点击此链接:meta.stackexchange.com/questions/5234/…
    【解决方案3】:

    我遇到了同样的问题并确定了另一个可能的原因(只是希望这可能对某人有所帮助)。

    我还使用自定义 ClassLoader 加载 Class 对象(带有 Jackson 注释的 DTO POJO),并让 ObjectMapper 和 SchemaFactoryWrapper 处理它们以生成相应的 JSON 模式。就我而言,我忘记将 jackson-annotations jar 添加到 ClassLoader 中,这会导致所描述的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 2022-08-09
      • 2016-04-14
      相关资源
      最近更新 更多