解决方案:添加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>
这是我正在尝试的一门课:
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);
}
}