Java App Engine - 使用适当的库验证 JSON 模式

Java App Engine - Validate JSON Schema with proper Library

我正在寻找可以与 App Engine Java 环境一起使用的有效 JsonSchema 验证库。

我找到了 3 个不同的库,但其中 none 个可用的原因不同。

第 1 位:json-schema-validator

<dependency>
    <groupId>org.kitchen-eel</groupId>
    <artifactId>json-schema-validator</artifactId>
    <version>1.5.2</version>
</dependency>

这是代码

ValidationReport report;
JsonNode schema = JsonLoader.fromURL(Validator.class.getResource("schema.json"));
JsonNode json = JsonLoader.fromString(input);

JsonSchemaFactory factory = JsonSchemaFactory.defaultFactory();
JsonSchema jsonSchema = factory.fromSchema(schema);

report = jsonSchema.validate(json);

if (!report.isSuccess()) {
    // throw error
}

问题: 它使用我无法使用的旧版本的 Guava (Guava 13.0.1),因为我的主要项目使用版本 19,但更重要的是,我使用了版本 13 中尚不存在的更新的 Guava 方法

java.lang.NoSuchMethodError: com.google.common.collect.FluentIterable.toImmutableSet()Lcom/google/common/collect/ImmutableSet;
    at org.eel.kitchen.jsonschema.syntax.draftv4.DraftV4TypeSyntaxChecker.<clinit>(DraftV4TypeSyntaxChecker.java:43)

2号:org.everit.json.schema

<dependency>
    <groupId>org.everit.json</groupId>
    <artifactId>org.everit.json.schema</artifactId>
    <version>1.3.0</version>
</dependency>

这是代码

try (InputStream inputStream =  Validator.class.getResourceAsStream("schema.json")) {
    JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
    Schema schema = SchemaLoader.load(rawSchema);
    schema.validate(new JSONObject(input)); // throws a ValidationException if this object is invalid
}

问题:此库仅针对Java 8 编译,不适用于App Engine 环境

java.lang.UnsupportedClassVersionError: org/everit/json/schema/loader/SchemaLoader : Unsupported major.minor version 52.0

3号:json-schema-validator

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

这是代码

JsonNode schema = JsonLoader.fromURL(HelloAppEngine.class.getResource("schema.json"));
JsonNode json = JsonLoader.fromURL(HelloAppEngine.class.getResource("json.json"));

JsonSchema jsonSchema = JsonSchemaFactory.byDefault().getJsonSchema(schema);
ProcessingReport report = jsonSchema.validate(json);

if (!report.isSuccess()) {
    // throw error;
}

问题: Jackson好像是用多线程的方式来执行代码,如果不通过GAE ThreadManager声明的话在App Engine中是禁止的

java.io.IOException: java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "modifyThreadGroup")

我没有找到该库的其他 Java 实现,我的所有尝试都没有成功。我工作的唯一方法是第一个使用 Guava 13 的解决方案(我禁用了我的代码,它使用更新的 Guava 函数,用自定义代码重写了功能)

最后...问题:除了我已经展示的可以与 App Engine 一起使用的库之外,是否有人拥有替代库? 如果可能的话,我想避免下载 Java8 库并将其重新编译为 7 版本,甚至更新第一个库 Guava 依赖项。

I want avoid downloading the Java8 library and recompile it for 7

@rduilhe 创建了在 java6 上编译的 everit 库的反向移植。您可以在本地编译它,尽管它是早期版本的端口:https://github.com/rdruilhe/json-schema

(免责声明:我是 everit-org/json-schema 的作者)