java 根据外部架构文件验证与 Jackson 的 JSON 架构合规性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31179086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:14:29  来源:igfitidea点击:

Validate JSON schema compliance with Hymanson against an external schema file

javajsonHymansonjsonschema

提问by madison54

I would like to use the Hymanson library (https://github.com/FasterXML/Hymanson) to deal with JSON files in Java, which are described by a JSON schema file.

我想使用 Hymanson 库(https://github.com/FasterXML/Hymanson)来处理 Java 中的 JSON 文件,这些文件由 JSON 模式文件描述。

Now, I would like to validate, if a parsed JSON complies with a JSON schema file, which is parsed by itself.

现在,我想验证解析的 JSON 是否符合 JSON 模式文件,该文件由自身解析。

There is a JSON schema module for Hymanson (https://github.com/FasterXML/Hymanson-module-jsonSchema). However, it appears to me that its primary focus is on creatinga JSON schema file from within Java.

Hymanson 有一个 JSON 模式模块(https://github.com/FasterXML/Hymanson-module-jsonSchema)。但是,在我看来,它的主要重点是从 Java 中创建JSON 模式文件。

What is a good way to validate a JSON schema in Java? - preferably using Hymanson, but I am also open to other solutions.

在 Java 中验证 JSON 模式的好方法是什么?- 最好使用 Hymanson,但我也愿意接受其他解决方案。

采纳答案by Martina

As far as I know Hymanson can only produce schemas for given types, but not do validation. There is json-schema-validatorbut it is no longer maintained.

据我所知,Hymanson 只能为给定类型生成模式,但不能进行验证。有json-schema-validator但不再维护。

回答by Ajay Kumar

1.) Add Dependency pom.xml :-

1.) 添加依赖 pom.xml :-

    <dependency>
        <groupId>com.github.fge</groupId>
        <artifactId>json-schema-validator</artifactId>
        <version>2.2.6</version>
    </dependency>

2.) NoSqlEntity is a meta data for entity that can reside in no-sql database.

2.) NoSqlEntity 是实体的元数据,可以驻留在 no-sql 数据库中。

Initialized NoSqlEntity with schema file.

使用架构文件初始化 NoSqlEntity。

public static final NoSqlEntity entity = new NoSqlEntity("PAYOUT_ENTITY", "DB_","/schema/payout_entity.json");

public static final NoSqlEntity entity = new NoSqlEntity("PAYOUT_ENTITY", "DB_","/schema/payout_entity.json");

public class NoSqlEntity {
private static final Map<String, NoSqlEntity> STORE = new HashMap<>();

private final AtomicLong seq = new AtomicLong(System.currentTimeMillis());
private IdentityGenerator identityGenerator;
private String entity;
private String collectionName;
private String jsonSchema;
private String idColumn = "id";
private String database;


public NoSqlEntity(String entity, String idColumn, String collectionPrefix,      String jsonSchema) {
    this.entity = entity;
    this.idColumn = idColumn;

    this.collectionName = collectionPrefix + "_" + entity;
    this.jsonSchema = jsonSchema;
    STORE.put(entity, this);
}

public NoSqlEntity(String collectionName, String jsonSchema) {
    this.collectionName = collectionName;
    this.jsonSchema = jsonSchema;
}

public static NoSqlEntity valueOf(String entityType) {
    return STORE.get(entityType);
}

public boolean isNotNullSchema() {
    return jsonSchema != null;
}
 ...
 // Other Getter/Setter properties and methods.
}

3.) Sample format of validation schema file of payout_entity.json-

3.) payout_entity.json-的验证模式文件的示例格式

   {
    "properties":{
          "txId":{"type":"string"}

    }
    "required" :["txId","currency"]
  }

4.) JsonSchemaManager - Validate the incoming JSON schema and cache the schema as well.

4.) JsonSchemaManager - 验证传入的 JSON 模式并缓存该模式。

public class JsonSchemaManager {
private final static Logger LOGGER = LoggerFactory.getLogger(JsonSchemaManager.class);
protected final static String LS = StandardSystemProperty.LINE_SEPARATOR.value();

private final JsonValidator validator = JsonSchemaFactory.byDefault().getValidator();
private final Map<NoSqlEntity, JsonNode> schemaMap = new HashMap<>();

public JsonNode load(NoSqlEntity noSqlEntity) throws IOException {
    final JsonNode schema = JsonLoader.fromURL(this.getClass().getResource(noSqlEntity.getJsonSchema()));
    schemaMap.put(noSqlEntity, schema);
    return schema;
}

public void validateSchema(NoSqlEntity noSqlEntity, JsonNode toBeValidated, Consumer<ProcessingReport> consumer) {
    try {
        JsonNode schema = schemaMap.get(noSqlEntity);
        if (schema == null) {
            schema = load(noSqlEntity);
        }
        final ProcessingReport report = validator.validate(schema, toBeValidated);
        if (!report.isSuccess()) {
            consumer.accept(report);
        }
    } catch (IOException ex) { //NOSONAR
        throw new InvalidRequestException(ex.toString());
    } catch (ProcessingException ex) { //NOSONAR
        throw new InvalidRequestException(ex.toString());
    }
}

 public synchronized boolean synchronizedCheck(NoSqlEntity noSqlEntity, JsonNode toBeValidated, Consumer<Map<String, Object>> messageConsumers) {
    boolean flags = CommonUtils.unchecked(() -> {
        validateSchema(noSqlEntity, toBeValidated, report -> {
            report.forEach(processingMessage -> messageConsumers.accept(JsonConverter.jsonAsMapObject(processingMessage.asJson())));
        });
        return true;
    }, ex -> {
        throw new RuntimeException(ex.toString()); //NOSONAR
    });
    return flags;
}
}

5.) NoSqlRepository which persist meta data into NoSql DB.

5.) NoSqlRepository 将元数据保存到 NoSql DB 中。

@Component
public class NoSqlRepository {
private static final Logger LOGGER = LoggerFactory.getLogger(NoSqlRepository.class);
private final DocumentFormat documentFormat = DocumentFormat.JSON;
private static final String SEPARATOR = ",";

private static final ThreadLocal<MyLocalVariable> THREAD_LOCAL_VARIABLES = ThreadLocal.withInitial(() -> new MyLocalVariable());


static class MyLocalVariable {
    private JsonSchemaManager schemaManager = new JsonSchemaManager();
    private BasicBSONDecoder bsonDecoder = new BasicBSONDecoder();

    public JsonSchemaManager getSchemaManager() {
        return schemaManager;
    }

    public BasicBSONDecoder getBsonDecoder() {
        return bsonDecoder;
    }
}

private void checkSchemaIfAny(NoSqlEntity noSqlEntity, JsonNode entity) {
    if (noSqlEntity.isNotNullSchema()) {
        THREAD_LOCAL_VARIABLES.get().getSchemaManager().check(noSqlEntity, entity);
    }
}

public String saveEntity(NoSqlEntity noSqlEntity, JsonNode entity){
  // Before persisting payload into noSQL, validate payload against schema.
  this.checkSchemaIfAny(noSqlEntity,entity);
}
// Other CURD methods here...
}

回答by Matthias Wiedemann

Just stumbled about https://github.com/leadpony/justifyanother implementation of a validator for json schema, also more recent draft versions. (7,6,4)

只是偶然发现了https://github.com/leadpony/justify另一个 json 模式验证器的实现,也是最近的草稿版本。(7,6,4)