使用JsonSchema验证Json时未找到类异常

实际的

我是Java新手。

我正在尝试使用JsonSchema验证Json。我已经提到Java / Android-针对String模式验证String JSON ..我尝试使用#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类,但是却遇到了异常。

Thegauravmahawar

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

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

它清楚地表明error是由JVM尝试加载但由于不在类路径中而找不到它JsonParseException的包的类所引起的com.fasterxml.jackson.core

只需将com.fasterxml.jackson.corejar文件添加到您的类路径中即可。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

复选框未找到怪异的Java类异常

来自分类Dev

使用jQuery Ajax上传文件时出现“未找到多部分边界”异常

来自分类Dev

如何避免Android中未找到类的异常?

来自分类Dev

Laravel IronMQ类未找到异常

来自分类Dev

Java-未找到类异常

来自分类Dev

独立Spark上的JavaWordCount-未找到类异常

来自分类Dev

defrecord类未找到异常

来自分类Dev

当我从最新的2.0.3 Beta jar使用Mockito时,出现了org.objenesis.ObjenesisStd类的未找到类的异常

来自分类Dev

Maven-未找到类异常

来自分类Dev

libgdx-Intellij类未找到异常?

来自分类Dev

打开HFT纪事表,Websphere上未找到类异常

来自分类Dev

Hadoop中未找到类异常

来自分类Dev

使用Firebase登录用户时出现“身份验证/用户未找到”

来自分类Dev

java Maven类未找到异常

来自分类Dev

未找到类定义异常

来自分类Dev

Laravel IronMQ类未找到异常

来自分类Dev

Java-未找到类异常

来自分类Dev

运行JUnit测试时“未找到类”

来自分类Dev

SceneBuilder CellFactory类未找到异常

来自分类Dev

defrecord类未找到异常

来自分类Dev

MySql-Java-未找到类异常

来自分类Dev

未找到应用程序类从Google Play下载时发生异常

来自分类Dev

Java Play框架中未找到类异常

来自分类Dev

使用AppIdentityService进行身份验证时未找到电子表格

来自分类Dev

打开HFT纪事表,Websphere上未找到类异常

来自分类Dev

使用Firebase登录用户时出现“身份验证/用户未找到”

来自分类Dev

Composer 自动加载类未找到异常

来自分类Dev

使用自定义cordova插件的类未找到异常

来自分类Dev

成功包含时未找到类