0

我是 Java 新手。

我正在尝试使用 JsonSchema 验证 Json。我已经提到了Java/Android - Validate String JSON against String schema .. 我已经尝试过使用#Tihamer 代码,但我得到了一个我没有使用过的 JsonParserException。

下面是我的代码

import java.util.Iterator;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.report.ProcessingMessage;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;

public class JsonValidation {
    public static void main(String[] args)
    {
        JsonValidation jv = new JsonValidation();
        String jsonData = "{\"dispatcherMode\":\"standard\",\"noOfdispatcher\":\"3\",\"dispatcherInfo\":[{\"dispatcher\":\"Dispatcher 0 = 156.95.53.243:6108<--current\"},{\"dispatcher\":\"Dispatcher 1 = 156.95.53.220:6108\"},{\"dispatcher\":\"Dispatcher 2 = 172.26.41.113:6108\"}],\"noOfAuthServer\":\"3\",\"connected\":\"3\",\"authorizationInfo\":[{\"authServer\":\"authserver 0 = 172.26.41.114:6115(connected)<--current\",\"requests\":\"1503\",\"failures\":\"1\",\"queued\":\"0\",\"delay\":\"10225\"},{\"authServer\":\"authserver 1 = 156.95.53.220:6115(connected)\",\"requests\":\"10745\",\"failures\":\"0\",\"queued\":\"0\",\"delay\":\"5762\"},{\"authServer\":\"authserver 2 = 172.26.41.113:6115(connected)\",\"requests\":\"12545\",\"failures\":\"1\",\"queued\":\"0\",\"delay\":\"9756\"}],\"noOfCache\":\"2\",\"cacheInfo\":[{\"cacheNumber\":\"cache 0\",\"request\":\"0\",\"hits\":\"0\",\"entries\":\"0\",\"size\":\"10000\",\"ttl\":\"600sec\"},{\"cacheNumber\":\"cache 1\",\"request\":\"1\",\"hits\":\"1\",\"entries\":\"1\",\"size\":\"12000\",\"ttl\":\"300sec\"}]}";
        String jsonSchema = "{\"type\": \"object\",\"properties\": {\"dispatcherMode\": {\"type\": \"string\"},\"noOfdispatcher\": {\"type\": \"string\"},\"dispatcherInfo\": {\"type\": \"array\",\"items\": [{\"type\": \"object\",\"properties\": {\"dispatcher\": {\"type\": \"string\"}}},{\"type\": \"object\",\"properties\": {\"dispatcher\": {\"type\": \"string\"}}},{\"type\": \"object\",\"properties\": {\"dispatcher\": {\"type\": \"string\"}}}]},\"noOfAuthServer\": {\"type\": \"string\"},\"connected\": {\"type\": \"string\"},\"authorizationInfo\": {\"type\": \"array\",\"items\": [{\"type\": \"object\",\"properties\": {\"authServer\": {\"type\": \"string\"},\"requests\": {\"type\": \"string\"},\"failures\": {\"type\": \"string\"},\"queued\": {\"type\": \"string\"},\"delay\": {\"type\": \"string\"}}},{\"type\": \"object\",\"properties\": {\"authServer\": {\"type\": \"string\"},\"requests\": {\"type\": \"string\"},\"failures\": {\"type\": \"string\"},\"queued\": {\"type\": \"string\"},\"delay\": {\"type\": \"string\"}}},{\"type\": \"object\",\"properties\": {\"authServer\": {\"type\": \"string\"},\"requests\": {\"type\": \"string\"},\"failures\": {\"type\": \"string\"},\"queued\": {\"type\": \"string\"},\"delay\": {\"type\": \"string\"}}}]},\"noOfCache\": {\"type\": \"string\"},\"cacheInfo\": {\"type\": \"array\",\"items\": [{\"type\": \"object\",\"properties\": {\"cacheNumber\": {\"type\": \"string\"},\"request\": {\"type\": \"string\"},\"hits\": {\"type\": \"string\"},\"entries\": {\"type\": \"string\"},\"size\": {\"type\": \"string\"},\"ttl\": {\"type\": \"string\"}}},{\"type\": \"object\",\"properties\": {\"cacheNumber\": {\"type\": \"string\"},\"request\": {\"type\": \"string\"},\"hits\": {\"type\": \"string\"},\"entries\": {\"type\": \"string\"},\"size\": {\"type\": \"string\"},\"ttl\": {\"type\": \"string\"}}}]}},\"required\": [\"dispatcherMode\",\"noOfdispatcher\",\"dispatcherInfo\",\"noOfAuthServer\",\"connected\",\"authorizationInfo\",\"noOfCache\",\"cacheInfo\"]}";
        jv.validation(jsonData, jsonSchema);
    }


    public boolean validation(String  jsonData, String jsonSchema)
    {
        ProcessingReport report = null;
        boolean result = false;
        try{
            JsonNode schemaNode = JsonLoader.fromString(jsonSchema);
            JsonNode dataNode = JsonLoader.fromString(jsonData);
            JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
            JsonSchema schema = factory.getJsonSchema(schemaNode);
            report = schema.validate(dataNode);
        }
        catch(Exception ex){
            ex.printStackTrace();
        }


        if (report != null) {
            Iterator<ProcessingMessage> iter = report.iterator();
            while (iter.hasNext()) {
                ProcessingMessage pm = iter.next();
                System.out.println("Processing Message: "+pm.getMessage());
            }
            result = report.isSuccess();
        }
        System.out.println(" Result=" +result);
        return result;
    }

}

以下是错误

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonParseException
    at com.github.fge.jackson.JsonLoader.<clinit>(JsonLoader.java:50)
    at JsonValidation.validation(JsonValidation.java:25)
    at JsonValidation.main(JsonValidation.java:16)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonParseException
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 3 more

我解决了以前的错误,但现在我收到了类似的错误

Exception in thread "main" java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z
    at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:537)
    at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:448)
    at com.github.fge.jackson.JacksonUtils.newMapper(JacksonUtils.java:155)
    at com.github.fge.jackson.JacksonUtils.<clinit>(JacksonUtils.java:55)
    at com.github.fge.jackson.JsonNodeReader.<init>(JsonNodeReader.java:82)
    at com.github.fge.jackson.JsonLoader.<clinit>(JsonLoader.java:50)
    at com.github.fge.jsonschema.examples.Utils.loadResource(Utils.java:53)
    at JsonValidateTest.main(JsonValidateTest.java:19)

我没有使用 objectMapper 类,但我遇到了异常。

4

2 回答 2

0

请参阅堆栈跟踪中的以下部分:

Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonParseException
    at java.net.URLClassLoader$1.run(Unknown Source)
    ...

它清楚地表明这是由JVM 尝试加载但找不到它的包error的类引起的,因为它不在您的类路径中。JsonParseExceptioncom.fasterxml.jackson.core

只需com.fasterxml.jackson.core在类路径中添加 jar 文件即可。

于 2015-10-15T05:46:50.697 回答
0

添加 jackson-annotations.jar 从这里下载

如果是 maven 项目,请添加此依赖项。

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.6.3</version>
</dependency>

PS:每当你得到classnotfound,考虑你需要让那个类可用

仅供参考:公共类 ClassNotFoundException 扩展 ReflectiveOperationException

当应用程序尝试通过其字符串名称加载类时抛出:

  • Class 类中的 forName 方法。
  • ClassLoader 类中的 findSystemClass 方法。
  • ClassLoader 类中的 loadClass 方法。但找不到具有指定名称的类的定义。

来自文档

对于第二个错误:添加此(替换版本)在此处下载 jar并添加

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson.version}</version>
</dependency>
于 2015-10-15T06:11:02.563 回答