java - json 模式 validator NoClassDefFoundError : com/google/common/io/Closer

标签 java json jsonschema json-schema-validator

解决方案:添加guava依赖:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>18.0</version>
</dependency>

我在这里尝试使用 json-schema-validator:https://github.com/fge/json-schema-validator .

我正在使用以下 Maven 依赖项:

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

这是我正在尝试的类(class):

import java.io.IOException;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;

public class JsonSchemaTest {
    public static void main(String[] args) throws IOException {
        JsonNode jsonNode = JsonLoader.fromString("{\"a\":1}");
    }
}

这是我收到的错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/io/Closer
    at com.github.fge.jackson.JsonNodeReader.fromReader(JsonNodeReader.java:120)
    at com.github.fge.jackson.JsonLoader.fromReader(JsonLoader.java:179)
    at com.github.fge.jackson.JsonLoader.fromString(JsonLoader.java:192)
    at com.cisco.webex.squared.flume.JsonSchemaTest.main(JsonSchemaTest.java:10)
Caused by: java.lang.ClassNotFoundException: com.google.common.io.Closer
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 4 more

--- 编辑:

如果我将我的 maven 版本更改为 2.1.7,我可以执行 JsonNode jsonNode = JsonLoader.fromString("{\"a\":1}"); 但如果没有相同的 java.lang.NoClassDefFoundError: com/google/common/io/Closer 错误,我无法在此处创建工厂:

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.report.ProcessingReport;

public final class JsonSchemaTest {
    public static void main(final String[] args) throws IOException, ProcessingException {
         //JsonNode data = JsonLoader.fromString("{\"a\":1}");
        final JsonNode data = JsonLoader.fromString("{\"a\":1}");
        final JsonNode fstabSchema = JsonLoader.fromString("{\"type\":\"object\", \"properties\":{\"a\":{\"type\":\"number\"}}}");

        final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
        final JsonSchema schema = factory.getJsonSchema(fstabSchema);

        ProcessingReport report;

        report = schema.validate(data);
        System.out.println(report);
    }
}

最佳答案

您必须在项目中导入 guava 库。

http://www.java2s.com/Code/Jar/g/Downloadguava150rc1jar.htm

关于java - json 模式 validator NoClassDefFoundError : com/google/common/io/Closer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26170074/

相关文章:

java - 东方数据库。设置 ConflictStrategy 抛出异常

java - 警报类型无关闭 javafx

javascript - jQuery/JSON 数据不在创建的 HTML 中循环

swagger - Swagger 2.0 中没有类型属性的架构对象

c# - Json.NET Schema IsValid 即使类型不同也返回 true

python - 验证 ip 地址的 JSON 架构不起作用

java - Spring Boot v1.5.3.RELEASE with Jersey 2 对所有请求 JSON 返回 404

java - 未配置嵌入式 jetty web 服务器 JSP 支持

javascript - 如何获得 Yelp Fusion API 访问 token ?

javascript - JSON.stringify 以 pretty-print 方式输出到 div