Java 使用JsonSchema验证Json时未发现类异常

Java 使用JsonSchema验证Json时未发现类异常,java,json,jsonschema,json-schema-validator,Java,Json,Jsonschema,Json Schema Validator,我是Java新手 我试图用JsonSchema验证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.re

我是Java新手

我试图用JsonSchema验证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;
    }

}
import java.util.Iterator;
导入com.fasterxml.jackson.databind.JsonNode;
导入com.github.fge.jackson.JsonLoader;
导入com.github.fge.jsonschema.core.report.ProcessingMessage;
导入com.github.fge.jsonschema.core.report.ProcessingReport;
导入com.github.fge.jsonschema.main.jsonschema;
导入com.github.fge.jsonschema.main.JsonSchemaFactory;
公共类JsonValidation{
公共静态void main(字符串[]args)
{
JsonValidation jv=新的JsonValidation();

字符串jsonData=“{\'dispatcherMode\”:\“standard\”,\“noOfdispatcher\”:\“3\”,\“dispatcherInfo\”:[{\'dispatcher\”:\“dispatcher 0=156.95.53.243:6108请参阅堆栈跟踪中的以下部分:

Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonParseException
    at java.net.URLClassLoader$1.run(Unknown Source)
    ...
它清楚地表明
错误是由包
com.fasterxml.jackson.core
的类
JsonParseException
引起的,JVM试图加载该包,但由于它不在类路径中,因此找不到它


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

添加jackson-annotations.jar 从下载

如果是maven项目,则添加此依赖性

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

com.fasterxml.jackson.core

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


com.fasterxml.jackson.core
杰克逊核心
${jackson.version}

Thank you@thegauravmahawar..你的回答帮助我解决了这个错误。你能帮我解决我在同一个问题中提到的另一个错误吗?谢谢你…我下载了jar文件并解决了这个错误..谢谢你的帮助检查我的第二次更新error@VedantKekan我的不是maven项目。那么我可以添加depen吗密度??检查已更新answer@VedantKekan谢谢你,问题解决了,唯一缺少的就是罐子,我把所有的罐子都加了进去,结果出来了。
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonParseException
    at java.net.URLClassLoader$1.run(Unknown Source)
    ...
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.6.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson.version}</version>
</dependency>