json-schema-validator NoClassDefFoundError:com / google / common / io / Closer

时间:2014-10-02 21:40:01

标签: java json jsonschema json-schema-validator

解决方案:添加番石榴依赖项:

<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>

这是我尝试的课程:

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);
    }
}

1 个答案:

答案 0 :(得分:2)

您必须在项目中导入番石榴库。

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