有没有办法通过java代码在draft-07 json模式中获得所需的字段数组

有没有办法通过java代码在draft-07 json模式中获得所需的字段数组,java,json,jsonschema,pojo,Java,Json,Jsonschema,Pojo,我想用必填字段的必需数组生成draft-04或draft-07的json模式 我不熟悉JSON模式,因此能够使用VictTools生成draft-07模式: 共享同一文件的代码: SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_7, OptionPreset.PLAIN_JSON); SchemaGeneratorConfig config = co

我想用必填字段的必需数组生成draft-04或draft-07的json模式

我不熟悉JSON模式,因此能够使用VictTools生成draft-07模式:

共享同一文件的代码:

SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_7, OptionPreset.PLAIN_JSON);
SchemaGeneratorConfig config = configBuilder.build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode jsonSchema = generator.generateSchema("MyClassName".class);

System.out.println(jsonSchema.toString());
我得到的o/p是:

draft-07的Json模式如下:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "ActiveOrHistoricCurrencyAndAmount": {
      "type": "object",
      "properties": {
        "ccy": { "type": "string" },
        "value": { "type": "number" }
      }
    }
  }
}
我想要的是:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "ActiveOrHistoricCurrencyAndAmount": {
      "type": "object",
      "properties": {
        "ccy": { "type":"string" },
        "value": { "type":"number" }
      },
      "required": ["ccy", "value"]
    }
  }
} 

我还需要强制字段的必需数组,因此如何使用java生成此数组?

victools generator4.17.0目前不支持Jackson注释
@JsonProperty(required=true)
required
属性。它计划在下一个版本中使用,并且可能会通过选项
JacksonOption.尊重\u JSONPROPERTY\u ORDER
设置为
true
启用。 看

在此之前,您可以自定义SchemaGeneratorConfigBuilder以支持以下属性:

SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_7, OptionPreset.PLAIN_JSON);

configBuilder.forFields()
       .withRequiredCheck(field -> {
            JsonProperty jsonProperty = field.getAnnotationConsideringFieldAndGetter(JsonProperty.class) ;
            if ( jsonProperty == null )
                 return false;  // No @JsonProperty ? => field not required.
            else
                 return jsonProperty.required(); // let's respect what the 'required' says
       });

SchemaGeneratorConfig config = configBuilder.build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode jsonSchema = generator.generateSchema("MyClassName".class);

(免责声明:我最近提交了此功能的PR)

您是否在输入中按要求标记了ccy属性(您没有提供)?是的..在pojo类中标记为required=true那么无论您使用何种工具生成架构,这都是一个问题。