代码之家  ›  专栏  ›  技术社区  ›  Deviling Master

Java应用程序引擎-使用适当的库验证JSON模式

  •  2
  • Deviling Master  · 技术社区  · 10 年前

    我正在寻找一个有效的JsonSchema验证库,它可以与AppEngineJava环境一起工作。

    我找到了3个不同的库,但由于不同的原因,没有一个可用。

    编号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中还没有的更新的Guavo方法

    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编译,而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函数,用自定义代码重写功能)

    最后……问题是:除了我已经展示的可能与AppEngine兼容的库之外,还有其他库吗? 如果可能的话,我希望避免下载Java8库并将其重新编译为7版本,甚至更新第一个库Guava依赖项。

    1 回复  |  直到 10 年前
        1
  •  2
  •   erosb    10 年前

    我想避免下载Java8库并重新编译7

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

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